繼承 將現實世界層次結構對應為類別關係:基類定義共性,衍生類別擴展特性。

上層特徵普遍,下層越具體並包含上層特徵。
object 為終極基類。virtual/override 實現多型。
class Vehicle {
public int Wheels; protected float Weight;
public Vehicle() {}
public Vehicle(int w, float g) { Wheels=w; Weight=g; }
public void Speak() => Console.WriteLine("Vehicle speaking!");
}
class Car : Vehicle {
int Passengers;
public Car(int w, float g, int p) : base(w,g) { Passengers=p; }
}
Vehicle 定義共性(輪子、重量),Car 繼承並新增載客特性。