תרשים המבנה (א') ואז מעקב מלא (ב') אחרי אותו קוד בדיוק — משלב מונים סטטיים חוצי-עצמים, סדר-קריאה של super בבנאים, והבדל מכריע בין פעולה שכן נדרסת (doIt) לפעולה שלא (print ב-B).
תרשים המבנה (א') ואז מעקב מלא (ב') אחרי אותו קוד בדיוק — משלב מונים סטטיים חוצי-עצמים, סדר-קריאה של super בבנאים, והבדל מכריע בין פעולה שכן נדרסת (doIt) לפעולה שלא (print ב-B).
class A
{
private static int countA = 0;
protected int n;
public A(int n1)
{
this.n = n1;
A.countA++;
System.out.println(A.countA + " " + this.n);
}
public int getN()
{
return this.n;
}
public void doIt()
{
for (int i = 0; i < this.n; i++)
System.out.print(i + ",");
System.out.println();
}
public void print()
{
System.out.println(this.n);
}
}
class B extends A
{
private static int countB = 0;
public B(int n2)
{
super(n2);
B.countB++;
System.out.println(B.countB + " " + n2);
}
public void doIt()
{
System.out.println("B Does It!");
}
}
class C extends B
{
private static int countC = 0;
private A a;
public C(int n1, int n2)
{
super(n1);
this.a = new A(n2);
countC++;
System.out.println("C is " + (a.getN() + this.n));
}
public void print()
{
super.print();
this.a.print();
}
}
class Test
{
private A[] arr;
public Test()
{
arr = new A[3];
arr[0] = new A(4);
arr[1] = new B(2);
arr[2] = new C(1, 6);
}
public void testIt()
{
for (int i = 0; i < arr.length; i++)
{
arr[i].doIt();
arr[i].print();
}
}
}
סעיף א
בנו תרשים מחלקות UML (הכולל קשרים) עבור ארבע המחלקות המוצגות בשאלה. על התרשים לכלול את שמות המחלקות ואת הקשרים ביניהן.
סעיף ב
לפניכם קטע קוד: public static void main(String[] args) { Test test = new Test(); test.testIt(); } עבור קטע קוד זה בצעו והציגו את פלט הקטע (חובה להציג מעקב).
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.