c# 模板方法模式

既然是模板肯定定义了相同的东西板式,提供了空白的地方自己添加进去就可以了

模板方法是把相同的部分抽象出来到抽象类中去定义,具体子类来实现具体的不同部分,这个思路也正式模板方法的实现精髓所在

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();

 

转载于:https://my.oschina.net/stuyun/blog/2254608

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值