返回
Featured image of post C♯ - 物件導向(Object-Oriented) - 繼承(Inheritance) 筆記

C♯ - 物件導向(Object-Oriented) - 繼承(Inheritance) 筆記

「繼承」則是描述依據現有類別來建立新類別的能力

[注意] 自訂類型(Custom types) - 結構(Struct) 不支援繼承

引用官方文件
引用官方文件

Example:

// WorkItem隱式繼承自Object
public class WorkItem
{
    // 建立 當前ID 用來運算使用
    private static int currentID;

    //Properties. 特性
    protected int ID { get; set; }
    protected string Title { get; set; }
    protected string Description { get; set; }
    protected TimeSpan jobLength { get; set; }

    // Default constructor. 預設構造函式
    public WorkItem()
    {
        ID = 0;
        Title = "Default title";
        Description = "Default description.";
        jobLength = new TimeSpan();
    }

    // Instance constructor. 實利構造函式
    public WorkItem(string title, string desc, TimeSpan joblen)
    {
        this.ID = GetNextID();
        this.Title = title;
        this.Description = desc;
        this.jobLength = joblen;
    }

    static WorkItem() => currentID = 0;

    protected int GetNextID() => ++currentID;

    public void Update(string title, TimeSpan joblen)
    {
        this.Title = title;
        this.jobLength = joblen;
    }

    // 繼承 ToString 的 Method 來自 System.Object
    public override string ToString() =>
        $"{this.ID} - {this.Title}";
}

// ChangeRequest 繼承來自 WorkItem 和 新增 originalItemID two constructors.
public class ChangeRequest : WorkItem
{
    protected int originalItemID { get; set; }

    // Default constructor for the derived class. 預設構造函式延伸的Class
    public ChangeRequest() { }

    // Instance constructor. 實利構造函式
    public ChangeRequest(string title, string desc, TimeSpan jobLen, int originalID)
    {
        this.ID = GetNextID();
        this.Title = title;
        this.Description = desc;
        this.jobLength = jobLen;
        this.originalItemID = originalID;
    }
}
WorkItem item = new WorkItem("Fix Bugs",
                            "Fix all bugs in my code branch",
                            new TimeSpan(3, 4, 0, 0));

ChangeRequest change = new ChangeRequest("Change Base Class Design",
                                        "Add members to the class",
                                        new TimeSpan(4, 0, 0),
                                        1);

Console.WriteLine(item.ToString());

change.Update("Change the Design of the Base Class",
    new TimeSpan(4, 0, 0));

Console.WriteLine(change.ToString());
/* Output:
    1 - Fix Bugs
    2 - Change the Design of the Base Class
*/

virtual 抽象 和 override 覆寫

virtual 是父層的 Method 可以用允許繼承的 Method 使用 override 將 Method 更改使用方式

abstract 抽象

防止使用 new Class 的話要使用 abstract

  • abstract Class 無法具現化。
  • abstract Class 可能包含 abstract Method 和存取子。
abstract class BaseClass   // Abstract class
{
    protected int _x = 100;
    protected int _y = 150;
    public abstract void AbstractMethod();   // Abstract method
    public abstract int X    { get; }
    public abstract int Y    { get; }
}

class DerivedClass : BaseClass
{
    public override void AbstractMethod()
    {
        _x++;
        _y++;
    }

    public override int X   // overriding property
    {
        get
        {
            return _x + 10;
        }
    }

    public override int Y   // overriding property
    {
        get
        {
            return _y + 10;
        }
    }

    static void Main()
    {
        var o = new DerivedClass();
        o.AbstractMethod();
        Console.WriteLine($"x = {o.X}, y = {o.Y}");
    }
}
// Output: x = 111, y = 161
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus