TemplateMethod

abstract通常在类里通常声明为protected

就想.net里面如果你想实现什么功能就得要实现某个接口重写某个方法,其实道理是一样的,都是模板方法

,直接看代码

 public abstract class Vehical
    {
        protected abstract void StartUp();
        protected abstract void Run();
        protected abstract void Turn( int degree );
        protected abstract void Stop();

        public void Test(int degree)
        {
            StartUp();
            Run();
            Turn( degree );
            Stop();
        }
    }
 public class HongqiCar:Vehical
    {
        protected override void StartUp()
        {
        }

        protected override void Run()
        {
        }

        protected override void Turn( int degree )
        {
        }

        protected override void Stop()
        {
        }
    }
 public class VehicalTestFramework
    {
        public static void DoTest( Vehical vehical )
        {
            vehical.Test( 45 );
        }
    }

调用:

 class Program
    {
        static void Main( string[] args )
        {
            VehicalTestFramework.DoTest( new HongqiCar() );
        }
    }

 

你可能感兴趣的:(template)