אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2022 · קיץ מועד א · שאלה 3כל שאלות המבחן ←
תכנות מונחה עצמיםפולימורפיזם ומחלקות מופשטות

A (תכונה מוגנת x, מתחילה מ-0, method מוסיפה, toString מדפיסה את x). B יורשת מ-A (y=1, method דורסת: מחסירה מ-x ומוסיפה ל-y, toString מדפיסה x.y). C יורשת מ-A (y=10, method דורסת: מוסיפה גם ל-x וגם ל-y, toString מדפיסה x/y). Test.main בונה מערך A[4]={{C,B,A,B}} ומדפיסה/מפעילה method(i+2) על כל תא.

public class A {
    protected int x;
    public A() {
        x = 0;
    }
    public void method(int num) {
        x += num;
    }
    public String toString() {
        return "" + x;
    }
}// end of class A

public class B extends A {
  private int y;
  public B()
  {
      super();
      y = 1;
  }
   public void method(int num)  {
      x -= num;
      y += num;
  }
  public String toString()  {
      return "" + x + "." + y;
  }
}// end of class B

public class C extends A {
  private int y;
  public C()
  {
      super();
      y = 10;
  }
  public void method(int num)  {
      x += num;
      y += num;
  }
  public String toString()  {
        return "" + x + "/"+ y;
  }
} // end of class C

public class Test
{
    public static void main (String [] args)
    {
        A [] array = new A[4];
        array[0] = new C();
        array[1] = new B();
        array[2] = new A();
        array[3] = new B();

        for (int i = 0; i < array.length; i ++)
        {
            System.out.println ("array[" + i + "] = " + array[i]);
            array[i].method(i+2);
            System.out.println ("array[" + i + "] = " + array[i]);
        }
    }
}
סעיף א

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

שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי