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

שני ממשקים (Int1,Int2) ושלוש מחלקות (One,Two,Three: One מממשת Int1; Two יורשת מ-One ודורסת doIt1 עם קריאה ל-super; Three יורשת מ-One ומממשת גם Int2, דורסת doIt1 ומוסיפה doIt2, שתיהן קוראות ל-super.doIt1()). חמישה קטעי קוד עצמאיים לחלוטין (השאלון מדגיש: 'אין קשר בין הסעיפים').

public interface Int1 { void DoIt1(); }
public interface Int2 { void DoIt2(); }

class One : Int1
{
    private int a;
    public One()
    {
        Console.WriteLine("ctor One");
        this.a = 0;
    }
    public virtual void DoIt1()
    {
        Console.WriteLine("One:DoIt1()");
        this.a += 5;
    }
    public override string ToString()
    {
        return "One: a=" + this.a;
    }
}
class Two : One
{
    private int b;
    public Two()
    {
        Console.WriteLine("ctor Two");
        this.b = 0;
    }
    public override void DoIt1()
    {
        Console.WriteLine("Two: doIt1()");
        base.DoIt1();
        this.b += 5;
    }
    public override string ToString()
    {
        return base.ToString() + " Two: b=" + this.b;
    }
}
class Three : One, Int2
{
    private int c;
    public Three()
    {
        Console.WriteLine("ctor1 Three");
        this.c = 0;
    }
    public override void DoIt1()
    {
        Console.WriteLine("Three: doIt1()");
        base.DoIt1();
        this.c += 5;
    }
    public void DoIt2()
    {
        Console.WriteLine("Three: doIt2()");
        base.DoIt1();
        this.c += 10;
    }
    public override string ToString()
    {
        return base.ToString() + " Three: c=" + this.c;
    }
}
סעיף א

Int2 x1 = new Int2(); x1.DoIt2(); Console.WriteLine(x1);

סעיף ב

Int1 x2 = new Three(); Int2 y2 = (Int2)x2; y2.DoIt2(); Console.WriteLine(x2); Console.WriteLine(y2);

סעיף ג

Three x3 = new One(); x3.DoIt1(); Console.WriteLine(x3);

סעיף ד

One x4 = new Two(); Three y4 = (Three)x4; y4.DoIt2(); Console.WriteLine(x4); Console.WriteLine(y4);

סעיף ה

Int2 x5 = new Three(); ((One)x5).DoIt1(); Console.WriteLine(x5);

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

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

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

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