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

A מגדירה one/two/four (two קוראת this.one; four קורא this.two). B יורשת A ודורסת one/two, ומוסיפה three() שקוראת super.two()/base.Two(). C יורשת B ודורסת four בלבד, ללא בונה משלה. שישה קטעי קוד בודקים הבנה של static vs dynamic binding, הטלות (cast) חוקיות/לא-חוקיות, ושרשרת בנאים.

public class A
{
    public A()
    {
        System.out.println("A");
    }

    public void one()
    {
        System.out.println("one of A");
    }

    public void two()
    {
        System.out.println("two of A");
        this.one();
    }

    public  void four()
    {
        System.out.println ("four of A");
        this.two();
    }
} //end of class A

public class B extends A
{
    public B()
    {
        System.out.println("B");
    }

    public void one()
    {
        System.out.println("one of B");
    }

    public void two()
    {
        System.out.println("two of B");
    }

    public void three()
    {
        System.out.println("three of B");
        super.two();
    }
} //end of class B

public class C extends B
{
    public  void four()
    {
        System.out.println ("four of C");
    }
} //end of class C

public class Test
{
    public static void main (String [] args)
    {
        // כאן יופיעו הפקודות שבסעיפים להלן
    }
}
סעיף א

A a1 = new A(); ((B) a1).two();

סעיף ב

B b1 = new B(); ((A) b1).three();

סעיף ג

A a2 = new A(); a2.one(); ((A) a2).two();

סעיף ד

A a3 = new B(); a3.two(); ((B) a3).two(); ((B) a3).three();

סעיף ה

B b2 = new B(); b2.two(); ((A) b2).two();

סעיף ו

B b3 = new C(); b3.four(); ((B)new C()).four();

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

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

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

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