abstract class AbstractClass { public void TemplateMethod() { PrimitiveOperation1(); PrimitiveOperation2(); } protected abstract void PrimitiveOperation1(); protected abstract void PrimitiveOperation2(); } public class MB{ public static void Show(){ AbstractClass abstractClass = new ConcreteClassA(); abstractClass.TemplateMethod(); Console.WriteLine(); abstractClass = new ConcreteClassB(); abstractClass.TemplateMethod(); } } class ConcreteClassA : AbstractClass { protected override void PrimitiveOperation1() { Console.WriteLine("ConcreteClassA.PrimitiveOperation1() called"); } protected override void PrimitiveOperation2() { Console.WriteLine("ConcreteClassA.PrimitiveOperation2() called"); } } class ConcreteClassB : AbstractClass { protected override void PrimitiveOperation1() { Console.WriteLine("ConcreteClassB.PrimitiveOperation1() called"); } protected override void PrimitiveOperation2() { Console.WriteLine("ConcreteClassB.PrimitiveOperation2() called"); } }
标签:Console,c#,void,PrimitiveOperation1,PrimitiveOperation2,WriteLine,protected,方法,模 From: https://www.cnblogs.com/Insist-Y/p/17393992.html