נתונות המחלקות 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 virtual void Foo() { this.x = 3; }
public virtual void Goo() { this.x = x + 3; }
public void Bar() { Goo(); }
public override string ToString() { return "x = " + this.x; }
}
public class BB : AA {
private int y;
public BB() : base() {
this.y = 2;
}
public BB (int a) : base (a) {
this.y = GetX() * 2;
}
public BB(int a, int b) : base(a) {
this.y = b;
}
public int GetY() { return this.y; }
public override void Foo() { this.y = 5; }
public override void Goo() { this.y = GetX() - 1; }
public void Bar (int a) {
this.y = a + GetX() + this.y;
}
public override string ToString() {
return base.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++)
Console.WriteLine(items[i]);
ציירו את העצמים שנוצרו, וכתבו מה הקוד מדפיס.
סעיף ב
לפניכם המשך קטע הקוד:
items[0].Foo();
items[1].Goo();
((AA) items[3]).Goo();
items[4].Bar();
BB temp = new BB(1);
temp.Bar();
Console.WriteLine("Temp: " + temp);
temp.Bar(9);
items[2] = temp;
for (int i = 0; i < items.Length; i++)
Console.WriteLine(items[i]);
כתבו מה הקוד מדפיס.
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.