接上章:
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");}
?
這一章 我們來說說 繼承的方法和方法隱藏。
C#委托?我們來修改代碼:
這個代碼比較尬,主要是演示子類中的方法使用父類的方法。
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");}
c語言繼承什么意思??實例化:
new A().ID();
?
結果
C#教程?結果是使用的A類專屬的ID方法,不再使用繼承的ID方法。
但是看代碼
?會提示報錯,為什么?
c#二維數組、因為子類的方法和父類的方法是相同。有可能會報錯,因為子類繼承了父類的方法,子類還擁有的相同的方法。此時不知道到底該運行那個方法。所以這個時候要做出抉擇。
這個抉擇基本就是規定了。
使用new關鍵字來隱藏基類成員或者方法。
那么問題來了,我要用父類的方法時候,該怎么辦?
繼承JAVA、嗯,可將子類轉父類。
((new A()) as NameList).ID();/*分割線*/var a = new A();(a as NameList).ID();
上述兩種方法都是將子類轉換父類來使用父類的方法。
?
值得一說 new關鍵字是隱藏基類成員,換句話說我在子類中使用和父類一樣的方法名和參數時,使用new時,其中的方法內可以實現和父類不一樣的代碼。
java單繼承多實現、?最終的代碼:
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 new 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");}
?
繼承的基本使用就是這樣子了