אתם מתרגלים שאלה מתוך בגרות מדעי המחשב — מבני נתונים (שאלון 899271)מבחן 2022 · קיץ מועד א · שאלה 17כל שאלות המבחן ←
תכנות מונחה עצמים›פולימורפיזם ומחלקות מופשטות

נתונות שלוש מחלקות – Mammal (יונק), Antelope (אנטילופה) ו-Beaver (בונה):

public class Mammal {
    protected int weight;
    public Mammal (int w) {
        weight = w;
    }
    public int GetWeight () {
        return weight;
    }
    public virtual bool IsSame (Mammal other) {
        Console.WriteLine ("In Mammal");
        return (this == other);
    }
}

public class Antelope : Mammal {
    public Antelope (int w) : base (w) {}
    public bool IsSame(Antelope other) {
        Console.WriteLine ("In Antelope");
        return ((other != null) && (this.weight == other.weight));
    }
}

public class Beaver : Mammal {
    public Beaver (int w) : base (w) {}
    public override bool IsSame (Mammal other) {
        Console.WriteLine ("In Beaver");
        return ((other != null) && (other is Beaver) && (this.weight == ((Beaver)other).weight));
    }
}

נתונה המחלקה Program:

public class Program {
    public static void Main (string[] args) {
        Antelope a1 = new Antelope (10);
        Object a2 = new Antelope (10);
        Beaver b1 = new Beaver (10);
        Mammal b2 = new Beaver (10);
        *******
    }
}

הציבו כל אחת מן השורות 1–10 שלהלן בפעולה Main, במקום שמסומן לעיל בכוכביות *******.

1. Console.WriteLine (a1.weight);
2. Console.WriteLine (((Beaver)a2).GetWeight());
3. Console.WriteLine (a1.IsSame (a2));
4. Console.WriteLine (a2.IsSame (a1));
5. Console.WriteLine (b1.IsSame (b2));
6. Console.WriteLine (b2.IsSame (b1));
7. Console.WriteLine (a1.IsSame ((Beaver)b2));
8. Console.WriteLine (a1.IsSame ((Antelope)a2));
9. Console.WriteLine (b1.IsSame ((Antelope)a2));
10. Console.WriteLine (b1.IsSame ((Beaver)a2));
סעיף א

כתבו במחברת את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין – כתבו את הפלט, ואם הוא לא תקין – הסבירו מדוע.

שאלות ותגובות על השאלה

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

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

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