אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2021 · קיץ מועד א · שאלה 6כל שאלות המבחן ←
תכנות מונחה עצמים›פולימורפיזם ומחלקות מופשטות

csharp

public class Apple { private int weight; public Apple (int w) { weight = w; } public int GetWeight () { return weight; } public bool Equals(Apple other) { return ((other!=null) && (weight == other.weight)); } } public class Banana { private int weight; public Banana (int w) { weight = w; } public int GetWeight () { return weight; } public override bool Equals (Object other) { return ((other != null) && (other is Banana) && (weight == ((Banana)other).weight)); } }

סעיף א

האם קיימת העמסה (Overloading) או דריסה (Overriding) של הפעולה equals במחלקות Apple ו-Banana? הסבירו את תשובתכם.

סעיף ב

נתונה המחלקה Program הבאה: public class Program { public static void Main (string[] args) { Console.WriteLine ("************"); Apple a1 = new Apple (10); object a2 = new Apple (10); Banana b1 = new Banana (10); object b2 = new Banana (10); ******* } } בהתייחס לכל אחת מהשורות הבאות, כתבו מה יקרה בעקבות הוספתה לשיטה Main שלעיל. הערה: אם הוספת פקודת קוד גורמת לשגיאה יש להסביר מהי השגיאה (שגיאת קומפילציה או שגיאת זמן ריצה).

  1. Console.WriteLine (a1.weight);
  2. Console.WriteLine (((Banana)a2).GetWeight());
  3. Console.WriteLine (a1.Equals(a2));
  4. Console.WriteLine (a2.Equals(a1));
  5. Console.WriteLine (b1.Equals(b2));
  6. Console.WriteLine (b2.Equals(b1));
  7. Console.WriteLine (a1.Equals((Banana)b2));
  8. Console.WriteLine (a1.Equals((Apple)a2));
  9. Console.WriteLine (b1.Equals((Apple)a2));
  10. Console.WriteLine (b1.Equals((Banana)a2));
שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי