抽象類和介面(抽象類部分實現,介面隱式實現)
07-18
namespace 介面和抽象類{ /// <summary> /// 載客 /// </summary> interface IpassengerCarrierable { void Passenger(); } /// <summary> /// 載貨 /// </summary> interface IHeavyLoadCarrierable { void Cargo(); } /// <summary> /// 車類 /// </summary> abstract class Vehicle { public string Color { get; set; } public string Type { get; set; } public double Money { get; set; } public abstract void Start(); public abstract void Run(); public abstract void Stop(); public void CanRunInRoad() { Console.WriteLine("該工具正行駛在公路上!"); } } abstract class Car : Vehicle { public override void Start() { Console.WriteLine("該轎車正在啟動中!"); } public override void Run() { Console.WriteLine("該轎車正在行駛中!"); } public override void Stop() { Console.WriteLine("該轎車正在拋錨中!"); } } abstract class Train : Vehicle { public override void Start() { Console.WriteLine("火車發動中!"); } public override void Run() { Console.WriteLine("火車運行中!"); } public override void Stop() { Console.WriteLine("火車停止運行了!"); } } class Compact : Car,IpassengerCarrierable { public void Passenger() { Console.WriteLine("小polo正在載客!"); } } class FreightTrain : Train, IHeavyLoadCarrierable { public void Cargo() { Console.WriteLine("該趟列車正在載貨中!"); } } class Program { static void Main(string[] args) { Compact c= new Compact(); c.Color = "黃色"; c.Type = "緊湊型小polo"; c.Money = 120000; Console.WriteLine("這輛{0}{1}大概{2}錢,",c.Color ,c.Type ,c.Money); c.CanRunInRoad(); c.Start(); c.Run(); c.Passenger(); c.Stop(); Console.WriteLine(); FreightTrain f = new FreightTrain(); f.Color = "綠色"; f.Type = "載貨列車"; f.Money = 1000000; Console.WriteLine("這輛{0}{1}大概{2}錢,", f.Color, f.Type, f.Money); f.Start(); f.Run(); f.Cargo(); f.Stop(); Console.ReadKey(); } }}
推薦閱讀:
※探秘:線纜巨頭亨通如何實現「從精益到智能」?
※俄烏關係實現突破性進展
※推進依法治國 實現民族復興(1)
※比爾蓋茨如何實現了投資多樣化
※離岸信託不止是為避稅 實現財富傳承有妙招