נתונות המחלקות A, B (B: 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)) Console.WriteLine(1); (2) if (b1.Equals(a1)) Console.WriteLine(2); (3) if (a1.Equals(ab)) Console.WriteLine(3); (4) if (ab.Equals(a1)) Console.WriteLine(4); (5) if (b1.Equals(ab)) Console.WriteLine(5); (6) if (ab.Equals(b1)) Console.WriteLine(6); (7) if (a1.Equals(a2)) Console.WriteLine(7); (8) if (b1.Equals(b2)) Console.WriteLine(8);
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.