נתונות המחלקות AA (תכונה x, בונים, getX, foo, goo, bar, toString) ו-BB extends AA (תכונה נוספת y, בונים, getY, foo/goo דורסות, bar(int) עומס-יתר, toString דורסת). יש לעקוב אחרי שני קטעי קוד רצופים ולקבוע מה כל אחד מדפיס בפועל, כולל ציור העצמים שנוצרו.
נתונות המחלקות AA (תכונה x, בונים, getX, foo, goo, bar, toString) ו-BB extends AA (תכונה נוספת y, בונים, getY, foo/goo דורסות, bar(int) עומס-יתר, toString דורסת). יש לעקוב אחרי שני קטעי קוד רצופים ולקבוע מה כל אחד מדפיס בפועל, כולל ציור העצמים שנוצרו.
public class AA {
private int x;
public AA() { this.x = 4; }
public AA(int x) { this.x = x; }
public int getX() { return this.x; }
public void foo() { this.x = 3; }
public void goo() { this.x = this.x + 3; }
public void bar() { goo(); }
public String toString() { return "x = " + this.x; }
}
public class BB extends AA {
private int y;
public BB() {
super();
this.y = 2;
}
public BB (int a) {
super (a);
this.y = getX() * 2;
}
public BB (int a, int b) {
super (a);
this.y = b;
}
public int getY() { return this.y; }
public void foo() { this.y = 5; }
public void goo() { this.y = getX() - 1; }
public void bar (int a) {
this.y = a + getX() + this.y;
}
public String toString() {
return super.toString() + " y = " + this.y;
}
}
סעיף א
לפניכם קטע קוד:
AA[] items = new AA[6];
items[0] = new AA();
items[1] = new BB();
items[2] = new AA(2);
items[3] = new BB(2);
items[4] = new BB(1, 22);
items[5] = items[4];
for (int i = 0; i < items.length; i++)
System.out.println(items[i]);
ציירו את העצמים שנוצרו, וכתבו מה הקוד מדפיס.
סעיף ב
לפניכם המשך קטע הקוד:
items[0].foo();
items[1].goo();
((AA) items[3]).goo();
items[4].bar();
BB temp = new BB(1);
temp.bar();
System.out.println("Temp: " + temp);
temp.bar(9);
items[2] = temp;
for (int i = 0; i < items.length; i++)
System.out.println(items[i]);
כתבו מה הקוד מדפיס.
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.