נתונות המחלקות A, B (B extends A), ומחלקת Driver עם ההוראה main שלפניך (עד לפני ***). לפניך שמונה הוראות אפשריות להצבה במקום ***; יש להציב כל אחת בנפרד (בחזרה למצב ההתחלתי בכל פעם).
נתונות המחלקות A, B (B extends 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 boolean equals (Object other) {
System.out.println("AObject");
if (other instanceof A)
return (this.val == ((A) other).val);
return false;
}
}
public class B extends A {
private String st;
public B () {
this.st = "B";
}
public B (String st, int val) {
super (val);
this.st = st;
}
public String getSt () {
return this.st;
}
public boolean equals (Object other) {
System.out.println("BObject");
if (other instanceof B)
return (this.st.equals(((B) other).st) && this.getVal () == ((B) other).getVal ());
return false;
}
public boolean equals (A other) {
System.out.println("BA");
if (other instanceof B)
return (this.st.equals(((B) other).st) && this.getVal () == ((B) other).getVal ());
return false;
}
public boolean equals (B other) {
System.out.println("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)) System.out.println(1); (2) if (b1.equals(a1)) System.out.println(2); (3) if (a1.equals(ab)) System.out.println(3); (4) if (ab.equals(a1)) System.out.println(4); (5) if (b1.equals(ab)) System.out.println(5); (6) if (ab.equals(b1)) System.out.println(6); (7) if (a1.equals(a2)) System.out.println(7); (8) if (b1.equals(b2)) System.out.println(8);
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.