1 / 20

מחלקות classes

מחלקות classes. תזכורת - Structure. סינטקס: Structure שם המבנה משתנה 1 משתנה 2 ... End Structure. דוגמא: Structure Oved Dim name As String Dim maskoret As Integer End Structure. מתאים כאשר רוצים לאגוד ביחד מספר משתנים שיש ביניהם קשר.

kasia
Télécharger la présentation

מחלקות classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. מחלקותclasses

  2. תזכורת - Structure סינטקס: Structure שם המבנה משתנה 1 משתנה 2 ... End Structure דוגמא: Structure Oved Dim name As String Dim maskoret As Integer End Structure מתאים כאשר רוצים לאגוד ביחד מספר משתנים שיש ביניהם קשר ואם רוצים להוסיף יכולת לבצע "פעילויות" שקשורות למבנה?

  3. מחלקה class • מחלקה משמשת לאיגוד קבוצה של משתנים ופעולות (הנקראות מתודות) • למחלקה יש אוסף של משתנים • המתודות של המחלקה פועלות על המשתנים של המחלקה • מתודה מיוחדת הנקראת בנאי (constructor) • מגדירה מה קורה כאשר נוצר אוביקט חדש של המחלקה

  4. main גישה לחלקי המחלקה • המשתנים מוגדרים כפרטיים Private • רק מתודות של המחלקה יכולות להשתמש בהם • המתודות מכירות את כל המשתנים של המחלקה • המתודות מוגדרות כפומביות Public • אפשר להשתמש במתודות מחוץ למחלקה מתודות מחלקה תכונות

  5. כתיבה/ שימוש במחלקה • מי שכותב את המחלקה • צריך להכיר את הפרוטוטייפ של המתודות • יודע איך המימוש של המתודות בוצע (הוא כתב את המימוש...) • מי שמשתמש בחלקה • צריך להכיר את הפרוטוטייפ של המתודות • לא צריך לדעת איך המימוש של המתודות בוצע

  6. סינטקס של מחלקה class Public Class שם המחלקה Privateמשתנה 1 Privateמשתנה 2 ... Public Sub New() PublicFunction 1 כותרת הפונקציה ….. Public Function2 כותרת הפונקציה….. … Public Sub1 כותרת סברוטינה….. Public Sub 2כותרת סברוטינה…. … End Class

  7. עובד כמחלקה PublicClassOved Private name AsString PrivatemaskoretAsInteger PublicSubNew() name = "noName" maskoret = 0 EndSub PublicFunctionGetName() AsString Return name EndFunction PublicSubSetName(ByValstrAsString) name = str EndSub EndClass

  8. שימוש במחלקה "עובד" מתוך "Main" Sub Main() DimovAsNewOved() Console.WriteLine("The oveds name is " & ov.GetName) ov.SetName("Ariella") Console.WriteLine("The oveds name is " & ov.GetName) 'ov.name = "Ariella" DOES NOT WORK!!! WHY? EndSub

  9. דוגמא נוספת – מחלקה לריבוע PublicClassSquare Private side AsDecimal PublicSubNew() side = 0 EndSub PublicFunctionGetArea() AsDecimal Return side * side EndFunction PublicFunctionGetPerimeter() AsDecimal Return side * 4 EndFunction PublicSubSetSide(ByVal x AsDecimal) side = x EndSub EndClass

  10. דוגמא נוספת – מחלקה לעיגול PublicClassCircle Private radius AsDecimal PublicSubNew() radius = 0 EndSub PublicFunctionGetArea() AsDecimal Return radius * Math.PI EndFunction PublicFunctionGetPerimeter() AsDecimal Return 2 * Math.PI * radius EndFunction PublicSubSetRadius(ByVal x AsDecimal) radius = x EndSub EndClass

  11. דוגמא לשימוש של המחלקות Sub Main() DimsqAsNewSquare Console.WriteLine("The area is " & sq.GetArea) 'sq.side = 4 doesn't work sq.SetSide(2.1) Console.WriteLine("The area is " & sq.GetArea) DimcirAsNewCircle Console.WriteLine("The area is " & cir.GetArea) cir.SetRadius(2) 'sq.radius = 4 doesn't work Console.WriteLine("The perimeter is " & cir.GetPerimeter) EndSub

  12. מבני הנתונים Stack, queue, linked listכמחלקות

  13. תזכורת –Stack (מחסנית) • שימוש בLIFO • LIFO (Last In, First Out) lists. • אפשר להוסיף רק בסוף הרשימה • PUSH • אפשר להוריד רק מסוף הרשימה • POP

  14. ModuleModule1 Function Count(ByVal list AsArrayList) AsInteger Returnlist.Count() EndFunction Sub Push(ByValvalAsObject, ByRef list AsArrayList) list.Add(val) EndSub Function Pop(ByVal list AsArrayList) AsObject Dim obj AsObject = list.Item(list.Count - 1) list.RemoveAt(list.Count - 1) Returnobj EndFunction Function Peek(ByVal list AsArrayList) AsObject Returnlist.Item(list.Count - 1) EndFunction SubMain() Dimtest AsNewArrayList() DimiAsInteger Fori = 0 To 4 Push(i, test) Next Console.WriteLine(Count(test)) Fori = 0 Totest.Count - 1 Dimnum AsInteger = Pop(test) Console.WriteLine(num) Next EndSub EndModule להמציא מחדש את הגלגל – ראינו בשיעור קודם (שימוש בפונקציות)

  15. PublicClassCStack Private list AsNewArrayList() PublicSubNew() EndSub PublicFunction Count() AsInteger Returnlist.Count() EndFunction PublicSub Push(ByValvalAsObject) list.Add(val) EndSub PublicFunction Pop() AsObject Dim obj AsObject = list.Item(list.Count - 1) list.RemoveAt(list.Count - 1) Returnobj EndFunction PublicFunction Peek() AsObject Returnlist.Item(list.Count - 1) EndFunction EndClass להמציא מחדש את הגלגלעם מחלקות – גירסה א

  16. שימוש בMAIN(אותו דבר כמו שימוש בStack ADT) Sub Main() Dim test AsNewCStack() DimiAsInteger Fori = 0 To4 test.Push(i) Next Console.WriteLine(test.Count) Fori = 0 Totest.Count-1 Dim num AsInteger = test.Pop() Console.WriteLine(num) Next EndSub

  17. PublicClassCStack Private index AsInteger Private list AsNewArrayList() PublicSubNew() index = -1 EndSub PublicFunction Count() AsInteger Returnlist.Count() EndFunction PublicSub Push(ByValvalAsObject) list.Add(val) index += 1 EndSub PublicFunction Pop() AsObject Dim obj AsObject = list.Item(index) list.RemoveAt(index) index -= 1 Returnobj EndFunction PublicFunction Peek() AsObject Returnlist.Item(index) EndFunction EndClass להמציא מחדש את הגלגלעם מחלקות – גירסהב

  18. תזכורת Queue (תור) • שימוש בFIFO • FIFO (First In, First Out) lists. • אפשר להוסיף רק בסוף הרשימה • Enqueue • אפשר להוריד רק מהתחלת הרשימה • Dequeue

  19. תרגיל: איך בונים QUEUE? • PublicClassCQueue • Private list AsNewArrayList() • PublicFunction Count() AsInteger • ??? • EndFunction • PublicSub Enqueue(ByVal val AsObject) • ??? • EndSub • PublicFunction Dequeue() AsObject • ??? • EndFunction • PublicFunction Peek() AsObject • ??? • EndFunction • EndClass

  20. פתרון אפשרי PublicClassCQueue Private list AsNewArrayList() PublicSubNew() EndSub PublicFunction Count() AsInteger Return list.Count() EndFunction PublicSub Enqueue(ByVal val AsObject) list.Add(val) EndSub PublicFunction Dequeue() AsObject Dim obj AsObject = list.Item(0) list.RemoveAt(0) Return obj EndFunction PublicFunction Peek() AsObject Return list.Item(0) EndFunction EndClass

More Related