שני ממשקים (Int1,Int2) ושלוש מחלקות (One,Two,Three: One מממשת Int1; Two יורשת מ-One ודורסת doIt1 עם קריאה ל-super; Three יורשת מ-One ומממשת גם Int2, דורסת doIt1 ומוסיפה doIt2, שתיהן קוראות ל-super.doIt1()). חמישה קטעי קוד עצמאיים לחלוטין (השאלון מדגיש: 'אין קשר בין הסעיפים').
שני ממשקים (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 implements Int1
{
private int a;
public One()
{
System.out.println("ctor One");
this.a = 0;
}
public void doIt1()
{
System.out.println("One:DoIt1()");
this.a += 5;
}
public String toString()
{
return "One: a=" + this.a;
}
}
class Two extends One
{
private int b;
public Two()
{
System.out.println("ctor Two");
this.b = 0;
}
public void doIt1()
{
System.out.println("Two: doIt1()");
super.doIt1();
this.b += 5;
}
public String toString()
{
return super.toString() + " Two: b=" + this.b;
}
}
class Three extends One implements Int2
{
private int c;
public Three()
{
System.out.println("ctor1 Three");
this.c = 0;
}
public void doIt1()
{
System.out.println("Three: doIt1()");
super.doIt1();
this.c += 5;
}
public void doIt2()
{
System.out.println("Three: doIt2()");
super.doIt1();
this.c += 10;
}
public String toString()
{
return super.toString() + " Three: c=" + this.c;
}
}
סעיף א
Int2 x1 = new Int2(); x1.doIt2(); System.out.println(x1);
סעיף ב
Int1 x2 = new Three(); Int2 y2 = (Int2)x2; y2.doIt2(); System.out.println(x2); System.out.println(y2);
סעיף ג
Three x3 = new One(); x3.doIt1(); System.out.println(x3);
סעיף ד
One x4 = new Two(); Three y4 = (Three)x4; y4.doIt2(); System.out.println(x4); System.out.println(y4);
סעיף ה
Int2 x5 = new Three(); ((One)x5).doIt1(); System.out.println(x5);
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.