שלוש מחלקות A→B→C עם מונים סטטיים נפרדים (countA/countB/countC), שדה הכלה (myA בתוך C), ובנאי B(int y) שאינו קורא super(x) במפורש (מלכודת). ארבעה סעיפי מעקב על אותו קטע קוד בסיס.
שלוש מחלקות A→B→C עם מונים סטטיים נפרדים (countA/countB/countC), שדה הכלה (myA בתוך C), ובנאי B(int y) שאינו קורא super(x) במפורש (מלכודת). ארבעה סעיפי מעקב על אותו קטע קוד בסיס.
public class A
{
private static int countA = 0;
protected int x;
public A()
{
countA++;
this.x = 0;
}
public A(int x)
{
countA++;
this.x = x;
}
public override string ToString()
{
return "A:" + this.x;
}
public virtual int DoIt(int z)
{
return this.x * z;
}
public static int GetCount() { return countA; }
}// end of class A
public class B : A
{
protected int y;
private static int countB = 0;
public B(int y)
{
this.y = y;
countB++;
}
public B(int x, int y):base(x)
{
countB++;
this.y = y;
}
public override string ToString()
{
return "B:" + base.ToString() + ":" + this.y;
}
public static new int GetCount() { return countB; }
public override int DoIt(int z)
{
return base.DoIt(z) + this.y;
}
} //end of class B
public class C : B
{
private A myA;
private static int countC = 0;
public C(int x, int y):base(x,y)
{
this.myA = new A(x + y);
countC++;
}
public static new int GetCount() { return countC; }
public override string ToString()
{
return "C:" + base.ToString() + " [" + this.myA + "]";
}
}// end of class C
ציירו תרשים UML שמייצג את הקשרים בין המחלקות A, B, C. יש לסמן ירושה באמצעות החץ (▷—) והכלה באמצעות הסימן (◆—).
נתון קטע קוד בפעולה הראשית במחלקה אחרת (אותו קוד, C#): A[] arr = new A[5]; arr[0] = new A(13); arr[1] = new B(5, 10); arr[2] = new C(2, 7); arr[3] = new C(3, 3); arr[4] = new B(5); עקבו אחרי ביצוע קטע קוד. יש לצייר את כל העצמים שנוצרו ולציין ערכי התכונות שלהם.
לקוד של סעיף ב' התווסף קוד הבא: Console.WriteLine("A objects: " + A.GetCount()); Console.WriteLine("B objects: " + B.GetCount()); Console.WriteLine("C objects: " + C.GetCount()); רשמו מה יהיה הפלט.
לקוד של סעיף ב' התווסף קוד הבא: for (int k=0; k <arr.Length; k++) Console.WriteLine(arr[k]); רשמו מה יהיה הפלט.
לקוד של סעיף ב' התווסף קוד הבא: for (int k=0; k <arr.Length; k++) if(arr[k] is B) Console.WriteLine(arr[k].DoIt(k+1)); רשמו מה יהיה הפלט.
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.