繼承(子類構造執行的過程)
繼承中子類構造的執行過程:
1.從Main函數跳轉到子類有參構造,但是不進入方法體,無論有無base(),都會跳轉到父類構造
2.跳轉到父類有參構造,執行構造體
3.執行完後,跳回到子類有參構造並執行構造體
4.執行完後,跳回到Main函數,對象執行完畢
示例代碼:
/// <summary>
/// 父類
/// </summary>
public class Car
{
public string Brand { get; set; } //品牌
public string Color { get; set; } //顏色
/// <summary>
/// 無參構造
/// </summary>
public Car()
{
}
/// <summary>
/// 有參構造
/// </summary>
/// <param name="brand"></param>
/// <param name="color"></param>
public Car(string brand,string color)
{
this.Brand = brand;
this.Color = color;
}
/// <summary>
/// 子類
/// </summary>
public class LittleCar:Car
{
public int Price { get; set; }
public LittleCar()
{
}
public LittleCar(string brand, string color, int price):base(brand,color)
{
//this.Brand = brand;
//this.Color = color;
this.Price = price;
}
}
/// <summary>
/// Main方法
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
LittleCar littlecar = new LittleCar("路虎","灰色",5000);
LittleCar littlecar2 = new LittleCar("路虎", "藍色", 500000);
}
繼承(子類構造執行的過程)