→ חזרה לעמוד הקודם
// תרגול

תרגול פעולות בונות בהורשה

מבני נתונים · 899271פעולות בונות בהורשה

פריטים בספרייה

לפניכם ארבע מחלקות. אחריהן מופיע קטע הקוד מתוך התוכנית הראשית.

Item

public class Item
{
    private int id;

    public Item() : this(100)
    {
        Console.WriteLine("Item");
    }

    public Item(int id)
    {
        this.id = id;
        Console.WriteLine("ID: " + this.id);
    }
}

Book

public class Book : Item
{
    private string title;

    public Book(string title)
    {
        this.title = title;
        Console.WriteLine(this.title);
    }

    public Book(int id, string title) : base(id)
    {
        this.title = title;
        Console.WriteLine(this.title);
    }
}

AudioBook

public class AudioBook : Book
{
    private int minutes;

    public AudioBook(int id, string title)
        : this(id, title, 60)
    {
        Console.WriteLine("AudioBook");
    }

    public AudioBook(int id, string title, int minutes)
        : base(id, title)
    {
        this.minutes = minutes;
        Console.WriteLine("Minutes: " + this.minutes);
    }
}

Magazine

public class Magazine : Item
{
    private int issueNumber;

    public Magazine(int issueNumber) : base(200 + issueNumber)
    {
        this.issueNumber = issueNumber;
        Console.WriteLine("Issue: " + this.issueNumber);
    }
}

התוכנית הראשית (Main)

Item item = new Item();
Book book = new Book("Atlas");
Item guide = new Book(125, "Guide");
AudioBook audio = new AudioBook(130, "Stories");
Magazine magazine = new Magazine(7);
Book podcast = new AudioBook(142, "Science", 45);
Item journal = new Magazine(12);

ענו במחברת

  1. עקבו אחר יצירת העצמים בתוכנית הראשית וכתבו את הפלט לפי סדר ההדפסה.
  2. ציירו את כל העצמים שנוצרו. ליד כל עצם ציינו את שם המשתנה שמצביע אליו, את טיפוס המשתנה ואת טיפוס העצם. בתוך העצם רשמו את כל התכונות וערכיהן, כולל התכונות שירש.
הורדת הקובץ המקוריPDF · תרגול פעולות בונות בהורשה