参考视频链接:https://www.bilibili.com/video/BV13b411b7Ht?p=27&vd_source=10065785c7e10360d831474364e0d3e3
代码的进化与重构,从基本代码的讲解到逐步抽象成抽象类和接口。
文章目录
- 最初定义
- 利用继承改进
- 对方法进一步改进
- 利用虚函数进行改进
- 利用抽象进行改进
- 利用接口进行改进
- 最后给出最终的完整代码:
最初定义
最初定义两个类Car 和 Truck
namespace InterfaceAPPLication
{class Car{public void Run(){Console.WriteLine("Car is Running");}public void Stop() {Console.WriteLine("Stopped");}}class Truck{public void Run(){Console.WriteLine("Truck is Running");}public void Stop(){Console.WriteLine("Stopped");}}
}
利用继承改进
两个类具有重复的代码Stop()方法,不符合代码不重复出现的规则,对代码进行改进,将两个类继承自一个类 Vehicle
namespace InterfaceAPPLication
{class Vehcile{public void Stop(){Console.WriteLine("Stopped");}}class Car:Vehcile{public void Run(){Console.WriteLine("Car is Running");}}class Truck{public void Run(){Console.WriteLine("Truck is Running");}}
}
对方法进一步改进
继续观察Run方法,对Run方法进行改进:
namespace InterfaceAPPLication
{class Vehcile{public void Stop(){Console.WriteLine("Stopped");}public void Run(string type){if (type == "Car"){Console.WriteLine("Car is Running");}else if(type == "Truck"){Console.WriteLine("Truck is Running");}}}class Car:Vehcile{}class Truck:Vehcile{}
}
利用虚函数进行改进
但是当有新的类从Vehcile派生的话,就需要多Vehicle.Run(string type)函数体进行扩充,不符合设计原则中的封闭原则。替代方式,使用virtual虚函数修饰Run方法,派生类对Run方法进行重写。
namespace InterfaceAPPLication
{class Vehcile{public void Stop(){Console.WriteLine("Stopped");}public virtual void Run(string type){Console.WriteLine("Vehcile is Running");}}class Car:Vehcile{public override void Run(string type){Console.WriteLine("Car is Running");}}class Truck:Vehcile{public override void Run(string type){Console.WriteLine("Truck is Running");}}
}
利用抽象进行改进
基类Vehicle的Run方法被派生类重新实现,所以Run方法的函数体是没有意义的,所以可以修改为abstract方法,同时 Vehicle类为抽象类;
namespace InterfaceAPPLication
{abstract class Vehcile{public void Stop(){Console.WriteLine("Stopped");}public abstract void Run(string type);}class Car:Vehcile{public override void Run(string type){Console.WriteLine("Car is Running");}}class Truck:Vehcile{public override void Run(string type){Console.WriteLine("Truck is Running");}}
}
Vehcile抽象类包含的方法不全是抽象方法,当一个抽象类全部都是抽象方法的时候,IVechile类就是一个全是抽象方法的类 。可以看到Vehcile :继承IVechile,其中重写了 Filled() 和 Stop()方法,没有重写Run(string type)方法,不需要声明,默认继承了,然后Run(string type)在Car和Truck类中实现。
abstract class IVechile{public abstract void Filled();public abstract void Run(string type);public abstract void Stop();}abstract class Vehcile : IVechile{public override void Stop(){Console.WriteLine("Stopped");}public override void Filled(){Console.WriteLine("Pay and Filled");}}class Car : Vehcile{public override void Run(string type){Console.WriteLine("Car is Running");}}class Truck : Vehcile{public override void Run(string type){Console.WriteLine("Truck is Running");}}
利用接口进行改进
IVechile类就是一个全是抽象方法的类 ,
abstract class IVechile{public abstract void Filled();public abstract void Run(string type);public abstract void Stop();}
抽象类的抽象方法需要被派生类实现,所以默认是public abstract, 改为接口后方法就不需要修饰符了:(接口的命名方式大写英文字母 I+name)
interface IVechile{void Filled();void Stop();void Run(string type);}
注意,在对接口中方法进行实现的时候,没有实现的方法,需要在抽象类中注明是待实现的抽象方法。
interface IVechile{void Filled();void Stop();void Run(string type);}abstract class Vehcile : IVechile{public void Stop(){Console.WriteLine("Stopped");}public void Filled(){Console.WriteLine("Pay and Filled");}abstract public void Run(string type);}
最后给出最终的完整代码:
using System;
using System.Collections;
using System.Data;namespace InterfaceAPPLication
{interface IVechile{void Filled();void Stop();void Run(string type);}abstract class Vehcile : IVechile{public void Stop(){Console.WriteLine("Stopped");}public void Filled(){Console.WriteLine("Pay and Filled");}abstract public void Run(string type);}class Car : Vehcile{public override void Run(string type){Console.WriteLine("Car is Running");}}class Truck : Vehcile{public override void Run(string type){Console.WriteLine("Truck is Running");}}class Executer{static void Main(string[] args){Vehcile car = new Car();car.Run("Car");car.Stop();Vehcile truck = new Truck();truck.Run("Truck");truck.Stop();}}
}
运行结果: