טוען...
טוען...
נתונות המחלקות A, B, C, D, E:
public class A
{
protected int x;
public A()
{
this.x = 9;
System.out.println("A. x = " + this.x);
}
public A(int x)
{
this.x = x;
System.out.println("A. x = " + this.x);
}
public int getX() { return this.x; }
public int foo() { return this.x; }
}
public class B extends A
{
public B() { super(); }
public B(int x) { super(x); }
public int foo() { return this.x + 1; }
}
public class C extends B
{
public C() { super(); }
public C(int x) { super(x); }
public int foo() { return this.x + 2; }
public int bar() { return this.x; }
}
public class D extends C
{
public D()
{
super();
this.x++;
System.out.println("D. x = " + this.x);
}
public D(int x)
{
super(x);
System.out.println("D. x = " + this.x);
}
public D(int x, int y)
{
super();
this.x = this.x + x + y;
System.out.println("D. x = " + this.x);
}
public int foo() { return this.x - 1; }
}
public class E extends C
{
public E() { super(); }
public int bar() { return this.x + 1; }
}סרטט תרשים הייררכייה בין המחלקות A, B, C, D, E.
לפניך כותרת הפעולה:
csharp1public static int GetType (Object m)
הפעולה מחזירה 1 אם m מטיפוס A, 2 אם m מטיפוס B, 3 אם m מטיפוס C, 4 אם m מטיפוס D ו-5 אם m מטיפוס E.
ממש את הפעולה.
הדרכה: כדי לבדוק את סוג העצם יש להיעזר בפעולות GetX, Bar, Foo.
אין להשתמש בפעולות is ו־as ובפעולות של המחלקה Object, ואין לשנות את המחלקות A, B, C, D, E.
הנח ש-m שייך לאחת מן המחלקות A, B, C, D, E ואינו null.
לפניך המחלקה Tester:
csharp1public class Tester
2{
3 public static void Main(string[] args)
4 {
5 A a1 = new B();
6 A a2 = new E();
7 A a3 = new D();
8 A a4 = new D(5);
9 A a5 = new D(3, 7);
10 }
11}
כתוב את הפלט של הפעולה Main.
נתונות המחלקות A, B, C, D, E:
public class A
{
protected int x;
public A()
{
this.x = 9;
System.out.println("A. x = " + this.x);
}
public A(int x)
{
this.x = x;
System.out.println("A. x = " + this.x);
}
public int getX() { return this.x; }
public int foo() { return this.x; }
}
public class B extends A
{
public B() { super(); }
public B(int x) { super(x); }
public int foo() { return this.x + 1; }
}
public class C extends B
{
public C() { super(); }
public C(int x) { super(x); }
public int foo() { return this.x + 2; }
public int bar() { return this.x; }
}
public class D extends C
{
public D()
{
super();
this.x++;
System.out.println("D. x = " + this.x);
}
public D(int x)
{
super(x);
System.out.println("D. x = " + this.x);
}
public D(int x, int y)
{
super();
this.x = this.x + x + y;
System.out.println("D. x = " + this.x);
}
public int foo() { return this.x - 1; }
}
public class E extends C
{
public E() { super(); }
public int bar() { return this.x + 1; }
}סרטט תרשים הייררכייה בין המחלקות A, B, C, D, E.
לפניך כותרת הפעולה:
csharp1public static int GetType (Object m)
הפעולה מחזירה 1 אם m מטיפוס A, 2 אם m מטיפוס B, 3 אם m מטיפוס C, 4 אם m מטיפוס D ו-5 אם m מטיפוס E.
ממש את הפעולה.
הדרכה: כדי לבדוק את סוג העצם יש להיעזר בפעולות GetX, Bar, Foo.
אין להשתמש בפעולות is ו־as ובפעולות של המחלקה Object, ואין לשנות את המחלקות A, B, C, D, E.
הנח ש-m שייך לאחת מן המחלקות A, B, C, D, E ואינו null.
לפניך המחלקה Tester:
csharp1public class Tester
2{
3 public static void Main(string[] args)
4 {
5 A a1 = new B();
6 A a2 = new E();
7 A a3 = new D();
8 A a4 = new D(5);
9 A a5 = new D(3, 7);
10 }
11}
כתוב את הפלט של הפעולה Main.