C# 繼承(4)

來源:互聯網
上載者:User

標籤:一個   類的方法   com   子類   關鍵字   必須   泛型方法   img   where   

接上章:

 class NameList    {        public NameList() => Console.WriteLine("這個是NameList的建構函式");        public NameList(string Name) => Console.WriteLine($"這個是NameList的重載建構函式,輸入的參數是{Name}");        ~NameList() => Debug.WriteLine("釋放NameList");        public string Name { get; set; }        public void ID() => Console.WriteLine($"我的id是{Name}");    }    class A : NameList    {        public A() : base() => Console.WriteLine("這是A類的初始化,也就是建構函式");        public A(string Name) : base(Name) =>Console.WriteLine($"這個是A的重載建構函式,輸入的參數是{Name}");        ~A() => Debug.WriteLine("釋放A");    }    class B : NameList    {        public B() : base() => Console.WriteLine("這是A類的初始化,也就是建構函式");        public B(string Name) => Console.WriteLine($"這個是B的重載建構函式,輸入的參數是{Name}");        ~B() => Debug.WriteLine("釋放B");    }

 

這一章 我們來說說 繼承的方法和方法隱藏。

我們來修改代碼:

這個代碼比較尬,主要是示範子類中的方法使用父類的方法。

A類的ShowType方法使用NameList的Show<T>(T type)方法。

class NameList    {        public NameList() => Console.WriteLine("這個是NameList的建構函式");        public NameList(string Name) => Console.WriteLine($"這個是NameList的重載建構函式,輸入的參數是{Name}");        ~NameList() => Debug.WriteLine("釋放NameList");        public string Name { get; set; }        public void ID() => Console.WriteLine($"我的id是{Name}");        public void Show<T>(T type) where T : NameList => Console.WriteLine(type.GetType().FullName);//泛型方法    }    class A : NameList    {        public A() : base() => Console.WriteLine("這是A類的初始化,也就是建構函式");        public A(string Name) : base(Name) =>Console.WriteLine($"這個是A的重載建構函式,輸入的參數是{Name}");        ~A() => Debug.WriteLine("釋放A");        public void ShowType() => base.Show<A>(this);    }    class B : NameList    {        public B() : base() => Console.WriteLine("這是A類的初始化,也就是建構函式");        public B(string Name) => Console.WriteLine($"這個是B的重載建構函式,輸入的參數是{Name}");        ~B() => Debug.WriteLine("釋放B");                    }

執行個體化代碼:

   new A().ShowType();

結果

 

上述代碼主要是說子類調用父類的方法,使用Base關鍵字。當然父類的方法必須是公用的方法。

 

上面的代碼還是比較尬的,趕緊進入下一個環節 繼承的隱藏方法

我們先修改代碼:

在A類中添加一個名為ID的方法。此時A類有自己的ID方法和繼承NameList的ID方法。

 class NameList    {        public NameList() => Console.WriteLine("這個是NameList的建構函式");        public NameList(string Name) => Console.WriteLine($"這個是NameList的重載建構函式,輸入的參數是{Name}");        ~NameList() => Debug.WriteLine("釋放NameList");        public string Name { get; set; }        public void ID() => Console.WriteLine($"我的id是{Name}");        public void Show<T>(T type) where T : NameList => Console.WriteLine(type.GetType().FullName);    }    class A : NameList    {        public A() : base() => Console.WriteLine("這是A類的初始化,也就是建構函式");        public A(string Name) : base(Name) =>Console.WriteLine($"這個是A的重載建構函式,輸入的參數是{Name}");        ~A() => Debug.WriteLine("釋放A");        public void ShowType() => base.Show<A>(this);        public void ID() => Console.WriteLine("這個ID方法是A類");    }    class B : NameList    {        public B() : base() => Console.WriteLine("這是A類的初始化,也就是建構函式");        public B(string Name) => Console.WriteLine($"這個是B的重載建構函式,輸入的參數是{Name}");        ~B() => Debug.WriteLine("釋放B");                    }

 執行個體化:

new A().ID();

 

結果

結果是使用的A類專屬的ID方法,不再使用繼承的ID方法。

但是看代碼

 會提示報錯,為什嗎?

因為子類的方法和父類的方法是相同。有可能會報錯。

C# 繼承(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.