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

נתונות המחלקות A, B (B extends A), ומחלקת Driver עם ההוראה main שלפניך (עד לפני ***). לפניך שמונה הוראות אפשריות להצבה במקום ***; יש להציב כל אחת בנפרד (בחזרה למצב ההתחלתי בכל פעם).

public class A {
    private int val;
    public A () {
        this.val = 1;
    }
    public A (int val) {
        this.val = val;
    }
    public int GetVal () {
        return this.val;
    }
    public virtual bool Equals (object other) {
        Console.WriteLine ("AObject");
        if (other is A)
            return (this.val == ((A) other).val);
        return false;
    }
}

public class B : A {
    private string st;
    public B () {
        this.st = "B";
    }
    public B (string st, int val) : base (val) {
        this.st = st;
    }
    public string GetSt () {
        return this.st;
    }
    public override bool Equals (object other) {
        Console.WriteLine ("BObject");
        if (other is B)
            return (this.st.Equals(((B) other).st) && this.GetVal () == ((B) other).GetVal ());
        return false;
    }
    public bool Equals (A other) {
        Console.WriteLine ("BA");
        if (other is B)
            return (this.st.Equals(((B) other).st) && this.GetVal () == ((B) other).GetVal ());
        return false;
    }
    public bool Equals (B other) {
        Console.WriteLine ("BB");
        return (this.st.Equals(other.st) && this.GetVal () == other.GetVal ());
    }
}

public class Driver {
    public static void Main (string[] args) {
        A a1 = new A ();
        A a2 = new A (5);
        A ab = new B ();
        B b1 = new B ("B", 1);
        B b2 = new B ("B", 5);
        // *** //
    }
}
סעיף א

צייר תרשים המראה את היררכיית הירושה בין המחלקות A,B, ועבור כל אחד מן העצמים שנוצרו בפעולה main (a1,a2,ab,b1,b2) סמן מהו הטיפוס הסטטי (המוצהר) שלו ומהו הטיפוס הדינמי (הממשי) שלו.

סעיף ב

לפניך שמונה הוראות. הצב כל אחת מן ההוראות במקום //***// בפעולה main של המחלקה Driver (כל פעם בנפרד, בחזרה למצב ההתחלתי), וכתוב מה יהיה הפלט שיודפס בעקבות הצבת כל אחת מן ההוראות: (1) if (a1.equals(b1)) System.out.println(1); (2) if (b1.equals(a1)) System.out.println(2); (3) if (a1.equals(ab)) System.out.println(3); (4) if (ab.equals(a1)) System.out.println(4); (5) if (b1.equals(ab)) System.out.println(5); (6) if (ab.equals(b1)) System.out.println(6); (7) if (a1.equals(a2)) System.out.println(7); (8) if (b1.equals(b2)) System.out.println(8);

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

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

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

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