לפניכם המחלקות A, B, C, D, E:
1public class A
2{
3 protected int x;
4
5 public A()
6 {
7 this.x = 9;
8 System.out.println("A. x = " + this.x);
9 }
10 public A(int x)
11 {
12 this.x = x;
13 System.out.println("A. x = " + this.x);
14 }
15 public int getX() { return this.x; }
16 public int foo() { return this.x; }
17}
18
19public class B extends A
20{
21 public B() { super(); }
22 public B(int x) { super(x); }
23 public int foo() { return this.x + 1; }
24}
25
26public class C extends B
27{
28 public C() { super(); }
29 public C(int x) { super(x); }
30 public int foo() { return this.x + 2; }
31 public int bar() { return this.x; }
32}
33
34public class D extends C
35{
36 public D()
37 {
38 super();
39 this.x++;
40 System.out.println("D. x = " + this.x);
41 }
42 public D(int x)
43 {
44 super(x);
45 System.out.println("D. x = " + this.x);
46 }
47 public D(int x, int y)
48 {
49 super();
50 this.x = this.x + x + y;
51 System.out.println("D. x = " + this.x);
52 }
53 public int foo() { return this.x - 1; }
54}
55
56public class E extends C
57{
58 public E() { super(); }
59 public int bar() { return this.x + 1; }
60}סרטטו תרשים הייררכייה בין המחלקות 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 (ב-Java: instanceof) ובפעולות של המחלקה 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:
1public class A
2{
3 protected int x;
4
5 public A()
6 {
7 this.x = 9;
8 System.out.println("A. x = " + this.x);
9 }
10 public A(int x)
11 {
12 this.x = x;
13 System.out.println("A. x = " + this.x);
14 }
15 public int getX() { return this.x; }
16 public int foo() { return this.x; }
17}
18
19public class B extends A
20{
21 public B() { super(); }
22 public B(int x) { super(x); }
23 public int foo() { return this.x + 1; }
24}
25
26public class C extends B
27{
28 public C() { super(); }
29 public C(int x) { super(x); }
30 public int foo() { return this.x + 2; }
31 public int bar() { return this.x; }
32}
33
34public class D extends C
35{
36 public D()
37 {
38 super();
39 this.x++;
40 System.out.println("D. x = " + this.x);
41 }
42 public D(int x)
43 {
44 super(x);
45 System.out.println("D. x = " + this.x);
46 }
47 public D(int x, int y)
48 {
49 super();
50 this.x = this.x + x + y;
51 System.out.println("D. x = " + this.x);
52 }
53 public int foo() { return this.x - 1; }
54}
55
56public class E extends C
57{
58 public E() { super(); }
59 public int bar() { return this.x + 1; }
60}סרטטו תרשים הייררכייה בין המחלקות 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 (ב-Java: instanceof) ובפעולות של המחלקה 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.