1. 繼承的類型編程
2.實現繼承安全
(1) 虛方法:ide
(2) 多態性:函數
(3) 隱藏方法:(儘可能不使用)spa
(4) 調用方法的基類版本:使用base關鍵字,在派生類中調用基類方法。code
(5) 抽象類和抽象方法:對象
// 抽象基類 Shape public abstract class Shape { public abstract void Resize(int width, int height); //抽象方法 } // 派生類 Ellipse public class Ellipse : Shape { public override void Resize(int width, int height) { Size.Width = width; Size.Height = height; } } //實例化 Shape s1 = new Ellipse();
(6) 密封類和密封方法:sealed修飾符blog
class MyClass: MyBaseClass { public sealed override void FinalMethod() { // implementation } } class DerivedClass: MyClass { public override void FinalMethod() //wrong!Will give compilation error { } }
(7) 派生類的構造函數:繼承
// 使用自動屬性初始化器初始化屬性 // 編譯器會建立一個默認的構造函數 public class Sharp { public Position Position { get; } = new Position(); public Size Size { get; } = new Size(); } // 派生類Ellipse public class Ellipse : Sharp { public Ellipse() : base() { } } // 實例化Ellipse類,構造函數調用順序爲 // Object 構造函數 -> Sharp 構造函數 -> Ellipse 構造函數
// 基類構造函數內進行賦值,非默認初始化 public class Shape { public Shape(int width, int height, int x, int y) { Size = new Size { Width = width, Height = height }; Position = new Position { X = x, Y = y }; } } // 派生類 public class Ellipse : Shape { // 派生類中建立構造函數,用構造函數初始化器初始化基類的構造函數 public Ellipse(int width, int height, int x, int y) : base(width, height, x, y) { } // 但願容許使用默認的構造函數建立Ellipse對象 public Ellipse() : base(width: 0, height: 0, x: 0, y: 0) { } }
3. 接口:interface索引
(1) 定義和實現接口
public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } }
// 派生類 SaverAccount public class SaverAccount : IBankAccount { private decimal _balance; public void PayIn(decimal amount) => _balance += amount; public bool Withdraw(decimal amount) { if (_balance >= amount) { _balance -= amount; return true; } WriteLine("Withdrawal attempt failed."); return false; } public decimal Balance => _balance; public override string ToString() => $"Venus Bank Saver: Balance = {_balance,6:C}"; } // 派生類 GoldAccount public class GoldAccount : IBankAccount { //etc }
IBankAccount[] accounts = new IBankAccount[2]; account[0] = new SaverAccount(); account[1] = new GoldAccount();
(2) 派生的接口
public interface ITransferBankAccount : IBankAccount { bool TransferTo(IBankAccount destination, decimal amount); }
public class CurrentAccount : ITransferBankAccount { private decimal _balance; public void PayIn(decimal amount) => _balance += amount; public bool Withdraw(decimal amount) { if (_balance >= amount) { _balance -= amount; return true; } WriteLine("Withdrawal attempt failed."); return false; } public decimal Balance => _balance; public bool TransferTo(IBankAccount destination, decimal amount) { bool result = Withdraw(amount); if (result) { destination.PayIn(amount); } return result; } public override string ToString() => $"Jupiter Bank Current Account: Balance = {_balance,6:C}"; }
4. is 和 as 運算符: 與繼承有關
public void WorkWithManyDifferentObject(object o) { IBankAccount account = o as IBankAccount; if (account != null) { //work with the account } }
public void WorkWithManyDifferentObject(object o) { if (o is IBankAccount) { IBankAccount account = (IBankAccount)o; //work with the account } }
注:文中代碼均來自《C#高級編程(第10版)C# 6 & .NET Core 1.0》