1 / 15

מחלקות classes

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

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. סינטקס של מחלקה class Public Class שם המחלקה Privateמשתנה 1 Privateמשתנה 2 ... Public Sub New() PublicFunction 1 כותרת הפונקציה ….. Public Function2 כותרת הפונקציה….. … Public Sub1 כותרת הפונקציה ….. Public Sub 2כותרת הפונקציה …. … End Class

  6. עובד כמחלקה Public Class Oved Private name As String Private maskoret As Integer Public Sub New() name = "noName" maskoret = 0 End Sub Public Function GetName() As String Return name End Function Public Sub SetName(ByVal str As String) name = str End Sub End Class

  7. שימוש במחלקה "עובד" מתוך "Main" Sub Main() Dim ov As New Oved() 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? End Sub

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

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

  10. להמציא מחדש את הגלגל – ראינו קודם (שימוש בפונקציות) Module Module1 Function Count(ByVal list As ArrayList) As Integer Return list.Count() End Function Sub Push(ByValval As Object, ByRef list As ArrayList) list.Add(val) End Sub Function Pop(ByVal list As ArrayList) As Object Dim obj As Object = list.Item(list.Count - 1) list.RemoveAt(list.Count - 1) Return obj End Function Function Peek(ByVal list As ArrayList) As Object Return list.Item(list.Count - 1) End Function … … Sub Main() Dim test As New ArrayList() Dim i As Integer For i = 0 To 4 Push(i, test) Next Console.WriteLine(Count(test)) For i = 0 To test.Count - 1 Dim num As Integer = Pop(test) Console.WriteLine(num) Next End Sub End Module

  11. Public Class CStack Private index As Integer Private list As New ArrayList() Public Sub New() index = -1 End Sub Public Function Count() As Integer Return list.Count() End Function Public Sub Push(ByVal val As Object) list.Add(val) index += 1 End Sub Public Function Pop() As Object Dim obj As Object = list.Item(index) list.RemoveAt(index) index -= 1 Return obj End Function Public Function Peek() As Object Return list.Item(index) End Function End Class להמציא מחדש את הגלגלעם מחלקות

  12. שימוש בMAIN(אותו דבר כמו שימוש בStack ADT) Sub Main() Dim test As New CStack() Dim i As Integer For i = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) For i = 1 To test.Count Dim num As Integer = test.Pop() Console.WriteLine(num) Next End Sub

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

  14. תרגיל: איך בונים QUEUE? Public Class CQueue Private list As New ArrayList() Public Function Count() As Integer ??? End Function Public Sub Enqueue(ByVal val As Object) ??? End Sub Public Function Dequeue() As Object ??? End Function Public Function Peek() As Object ??? End Function End Class

  15. פתרון אפשרי Public Class CQueue Private list As New ArrayList() Public Sub New() End Sub Public Function Count() As Integer Return list.Count() End Function Public Sub Enqueue(ByVal val As Object) list.Add(val) End Sub Public Function Dequeue() As Object Dim obj As Object = list.Item(0) list.RemoveAt(0) Return obj End Function Public Function Peek() As Object Return list.Item(0) End Function End Class

More Related