בי מאסטר (Bmaster)מאגר שאלות בגרותשאלות נפוצותמדריכיםארכיון בגרויות
תפריט

חומרי לימוד

מאגר שאלות בגרותשאלות נפוצותמדריכיםארכיון בגרויות
חזרה למאגר2021 קיץ מועד ב

ניתוח ירושה והכלה במחלקות

כולל סעיף סרטוט — בקרוב
שאלה קודמתשאלה הבאה
שאלה 14מבני נתונים2021 קיץ מועד ב

ניתוח ירושה והכלה במחלקות

לפניכם המחלקות A, B, C, D (ראו קוד) והמחלקה Tester:

java
1public class Tester {
2    public static void main(String[] args) {
3        A y1 = new A();
4        A y2 = new A();
5        y1.foo(y1.getNum());
6        System.out.println(y1.isEqual(y2));
7        B y3 = new B();
8        A y4 = new B();
9        System.out.println(y3.isEqual(y4));
10        System.out.println(y4.isEqual(y3));
11        B y5 = y3;
12        y5.setNum(0);
13        System.out.println(y3.isEqual(y5));
14        C y6 = new C();
15        System.out.println(((B)y4).isEqual(y6));
16        D d1 = new D(y4);
17        D d2 = new D(y6);
18        System.out.println(B.count);
19        d1.func();
20        d2.func();
21        d1.isB();
22        d2.isB();
23    }
24}
1public class A {
2    public static int count = 0;
3    protected int num;
4
5    public A() {
6        this.num = 3;
7        count++;
8    }
9    public int getNum() {
10        return this.num;
11    }
12    public void setNum(int num) {
13        this.num = num;
14    }
15    public void foo(int num) {
16        this.num = num;
17        num++;
18    }
19    public boolean isEqual(A other) {
20        return (this.num == other.num);
21    }
22    public void func() {
23        System.out.println("I am A");
24    }
25}
26
27public class B extends A {
28    public void func() {
29        System.out.println("I am B");
30    }
31    public boolean isEqual(B other) {
32        return (this == other);
33    }
34}
35
36public class C extends A { }
37
38public class D {
39    private A a;
40
41    public D(A a) {
42        this.a = a;
43    }
44    public void func() {
45        (this.a).func();
46    }
47    public void isB() {
48        System.out.println((this.a) instanceof B);
49    }
50}

משימות

אמשימה אdiagram

סרטטו מפת הייררכייה בין המחלקות A, B, C, D. יש לסמן ירושה באמצעות החץ (◁———) והכלה באמצעות הסימן (◆——).

במשימה בdiagram

סרטטו את העצמים שנוצרו בפעולה Main במחלקה Tester שלפניכם.

גמשימה גtext

כתבו את הפלט של הפעולה Main במחלקה Tester שלפניכם.

שאלה 14מבני נתונים2021 קיץ מועד ב

ניתוח ירושה והכלה במחלקות

לפניכם המחלקות A, B, C, D (ראו קוד) והמחלקה Tester:

java
1public class Tester {
2    public static void main(String[] args) {
3        A y1 = new A();
4        A y2 = new A();
5        y1.foo(y1.getNum());
6        System.out.println(y1.isEqual(y2));
7        B y3 = new B();
8        A y4 = new B();
9        System.out.println(y3.isEqual(y4));
10        System.out.println(y4.isEqual(y3));
11        B y5 = y3;
12        y5.setNum(0);
13        System.out.println(y3.isEqual(y5));
14        C y6 = new C();
15        System.out.println(((B)y4).isEqual(y6));
16        D d1 = new D(y4);
17        D d2 = new D(y6);
18        System.out.println(B.count);
19        d1.func();
20        d2.func();
21        d1.isB();
22        d2.isB();
23    }
24}
1public class A {
2    public static int count = 0;
3    protected int num;
4
5    public A() {
6        this.num = 3;
7        count++;
8    }
9    public int getNum() {
10        return this.num;
11    }
12    public void setNum(int num) {
13        this.num = num;
14    }
15    public void foo(int num) {
16        this.num = num;
17        num++;
18    }
19    public boolean isEqual(A other) {
20        return (this.num == other.num);
21    }
22    public void func() {
23        System.out.println("I am A");
24    }
25}
26
27public class B extends A {
28    public void func() {
29        System.out.println("I am B");
30    }
31    public boolean isEqual(B other) {
32        return (this == other);
33    }
34}
35
36public class C extends A { }
37
38public class D {
39    private A a;
40
41    public D(A a) {
42        this.a = a;
43    }
44    public void func() {
45        (this.a).func();
46    }
47    public void isB() {
48        System.out.println((this.a) instanceof B);
49    }
50}

משימות

אמשימה אdiagram

סרטטו מפת הייררכייה בין המחלקות A, B, C, D. יש לסמן ירושה באמצעות החץ (◁———) והכלה באמצעות הסימן (◆——).

במשימה בdiagram

סרטטו את העצמים שנוצרו בפעולה Main במחלקה Tester שלפניכם.

גמשימה גtext

כתבו את הפלט של הפעולה Main במחלקה Tester שלפניכם.