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》