Abstract classes and interfaces generated and used

    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.Run();
            car.Stop();
        }
    }
    class Vehicle
    {
        public virtual void Run()
        {
            Console.WriteLine("Vehicle is Running");
        }
        public virtual void Stop()
        {
            Console.WriteLine("Vehicle is Stopped");
        }
        public virtual void Full()
        {
            Console.WriteLine("Full Fuel");
        }
    }
    class Car:Vehicle
    {
        public override void Run()
        {
            Console.WriteLine("Car is Running");
        }

        public override void Stop()
        {
            Console.WriteLine("Car is Stopped");
        }
    }
}

We find this example, the method Vehicle class did not used before, and Vehicle is Running this argument too specific.
Therefore, we modify the Vehicle class, and consequently do not let the method in writing:

    class Vehicle
    {
        public virtual void Run() { }
        public virtual void Stop() { }
    }

But this is inappropriate and consequently do not write it, just put the method body also removed it, leaving only the method name
this the following code will not compile way of example only with oh

    class Vehicle
    {
        public virtual void Run() ;//这样是无法编译的
        public virtual void Stop() ;//这样是无法编译的
    }

Has always been virtual methods (virtual), now the method body did not, more true, so was born - abstract methods

    abstract class Vehicle
    {
        public abstract void Run();
        public abstract void Stop();
    }

When using the rewrite is still override, therefore only can be replaced without having to change.
An abstract class is designed to make the base class and students, because abstract classes can not be instantiated, even though the actual internal abstract class method, but also because there are abstract methods can not call all methods and error.
E.g:

    abstract class Vehicle
    {
        public abstract void Run();
        public abstract void Stop();
        public void full()
        {
            Console.WriteLine("Full Fuel");
        }
    }

Even if there is a specific method full (), still can not be instantiated.

So just say this particularly abstract, abstract to abstract class does not contain any actual method of

    abstract class Vehicle
    {
        public abstract void Run();
        public abstract void Stop();
    }

Guess you like

Origin www.cnblogs.com/maomaodesu/p/11612580.html