既然是模板肯定定义了相同的东西板式,提供了空白的地方自己添加进去就可以了
模板方法是把相同的部分抽象出来到抽象类中去定义,具体子类来实现具体的不同部分,这个思路也正式模板方法的实现精髓所在
public abstract class Vegetabel {
publac void ActionVegetabel(){
Console.WriteLine("模板开始");
VegetabelAction001();
VegetabelAction002();
/// 最后执行你实现方法是换台还是干嘛随便自己
VegetabelAction003();
}
public void VegetabelAction001(){
Console.WriteLine("插电");
}
public void VegetabelAction002(){
Console.WriteLine("打开电视");
}
///实现自子的方式方法
public abstract void VegetabelAction003()
}
//简单用法
public class Spinach001 : Vegetabel { public override void VegetabelAction003() { Console.WriteLine("我要换台"); } } Spinach001 A =new Spinach001 A.ActionVegetabel(); public class Spinach002 : Vegetabel { public override void VegetabelAction003() { Console.WriteLine("提高音量"); } } Spinach002 B =new Spinach002 B.ActionVegetabel();