טוען...
טוען...
נתונות שלוש מחלקות – Mammal (יונק), Antelope (אנטילופה) ו־Beaver (בונה), והמחלקה Program.
המחלקה Program נמצאת בחבילה (Package) שונה מן המחלקה Mammal.
public class Mammal {
protected int weight;
public Mammal(int w) {
weight = w;
}
public int getWeight() {
return weight;
}
public boolean isSame(Mammal other) {
System.out.println("In Mammal");
return (this == other);
}
}
public class Antelope extends Mammal {
public Antelope(int w) { super(w); }
public boolean isSame(Antelope other) {
System.out.println("In Antelope");
return ((other != null) && (this.weight == other.weight));
}
}
public class Beaver extends Mammal {
public Beaver(int w) { super(w); }
public boolean isSame(Mammal other) {
System.out.println("In Beaver");
return ((other != null) && (other instanceof Beaver) && (this.weight == ((Beaver)other).weight));
}
}
public class Program {
public static void main(String[] args) {
Antelope a1 = new Antelope(10);
Object a2 = new Antelope(10);
Beaver b1 = new Beaver(10);
Mammal b2 = new Beaver(10);
*******
}
}הציבו כל אחת מן השורות 1-10 שלהלן בפעולה main, במקום שמסומן בכוכביות *******.
כִּתבו את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא לא תקין — הסבירו מדוע.
System.out.println(a1.weight);System.out.println(((Beaver)a2).getWeight());System.out.println(a1.isSame(a2));System.out.println(a2.isSame(a1));System.out.println(b1.isSame(b2));System.out.println(b2.isSame(b1));System.out.println(a1.isSame((Beaver)b2));System.out.println(a1.isSame((Antelope)a2));System.out.println(b1.isSame((Antelope)a2));System.out.println(b1.isSame((Beaver)a2));נתונות שלוש מחלקות – Mammal (יונק), Antelope (אנטילופה) ו־Beaver (בונה), והמחלקה Program.
המחלקה Program נמצאת בחבילה (Package) שונה מן המחלקה Mammal.
public class Mammal {
protected int weight;
public Mammal(int w) {
weight = w;
}
public int getWeight() {
return weight;
}
public boolean isSame(Mammal other) {
System.out.println("In Mammal");
return (this == other);
}
}
public class Antelope extends Mammal {
public Antelope(int w) { super(w); }
public boolean isSame(Antelope other) {
System.out.println("In Antelope");
return ((other != null) && (this.weight == other.weight));
}
}
public class Beaver extends Mammal {
public Beaver(int w) { super(w); }
public boolean isSame(Mammal other) {
System.out.println("In Beaver");
return ((other != null) && (other instanceof Beaver) && (this.weight == ((Beaver)other).weight));
}
}
public class Program {
public static void main(String[] args) {
Antelope a1 = new Antelope(10);
Object a2 = new Antelope(10);
Beaver b1 = new Beaver(10);
Mammal b2 = new Beaver(10);
*******
}
}הציבו כל אחת מן השורות 1-10 שלהלן בפעולה main, במקום שמסומן בכוכביות *******.
כִּתבו את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא לא תקין — הסבירו מדוע.
System.out.println(a1.weight);System.out.println(((Beaver)a2).getWeight());System.out.println(a1.isSame(a2));System.out.println(a2.isSame(a1));System.out.println(b1.isSame(b2));System.out.println(b2.isSame(b1));System.out.println(a1.isSame((Beaver)b2));System.out.println(a1.isSame((Antelope)a2));System.out.println(b1.isSame((Antelope)a2));System.out.println(b1.isSame((Beaver)a2));