Getting Started with C # Comments (13)

For making the base class and the base classes and students open / close Principles

There are sections on inheritance begins to boot, the Code

   public class Program
    {
        static void Main(string[] args)
        {
            Vehicle vehicle = new Car();
            vehicle.Stopped();
        }
    }

    public class Vehicle
    {
       public void Stopped()
        {
            Console.WriteLine("Stop it");
        }
    }

    class Car : Vehicle
    {
     public  void Run()
        {
            Console.WriteLine("B");
        }
    }

     class RaceCar :Vehicle
    {
        public void Run()
        {
            Console.WriteLine("C");
        }
    }

In the code examples in our vehicle stopped only method, but can not call the car inside the run method, in order to solve this problem, we have two solutions:

Virtual methods:

   public class Program
    {
        static void Main(string[] args)
        {
            Vehicle vehicle = new Car();
            vehicle.Run();
            Vehicle vehicle_ = new RaceCar();
            vehicle_.Run();
        }
    }

    public class Vehicle
    {
       public void Stopped()
        {
            Console.WriteLine("Stop it");
        }
       public virtual void Run()
        {
            Console.WriteLine("...");        }
    }

    class Car : Vehicle
    {
     public override void Run()
        {
            Console.WriteLine("B");
        }
    }

     class RaceCar :Vehicle
    {
        public override void Run()
        {
            Console.WriteLine("C");
        }
    }

Adding a virtual method in the base class, subclass let go overwriting, which can effectively solve this problem.

In the Vehicle plus method:

    public class Vehicle
    {
       public void Stopped()
        {
            Console.WriteLine("Stop it");
        }
        public void Run(string typename)
        {
            if (typename=="Car")
            {
                Console.WriteLine("B");
            }
            else if (typename == "RaceCar")
            {
                Console.WriteLine("C");
            }
        }
    }

Violation of the principle of opening and closing, we change the class is off, this method does not work.

In the above two methods, we use the virtual method this solution, but not difficult to find, in practice, we may never call the Run method inside the Vehicle, so we can give him to become a pure virtual methods, using the key word abstract, has become a class corresponding abstract class.

    public abstract class Vehicle
    {
       public void Stopped()
        {
            Console.WriteLine("Stop it");
        }
        public abstract void Run();
    }

When the Vehicle which methods are virtual methods, it is pure abstract class, which is more common in C ++, in C # is Interface, named generally beginning with I

What are the interfaces and abstract classes

The product interfaces and abstract classes are software engineering

Specific categories - abstract class - Interface: more and more abstract, something fewer and fewer internal implementation

An abstract class is a class not fully achieved, the interface is completely unimplemented class (there may be achieved with the members and non-members of the public is not implemented, they represent a specific logic)

Abstract class multiplexing Health: exclusively used as a base class, also has a decoupling function

Determining the package, open uncertain, the subclasses to achieve delayed

The interface is not completely implement the logic "class" (pure virtual class: function members only; members are all public)

Decoupling interface born: highly cohesive easy coupling unit testing

Interface is a treaty, industrial production has long been known, there must be a division of labor and cooperation, there must be collaboration agreement

They can not be instantiated, can only be used to declare variables, reference to a concrete class (concrete class) Examples

 

 

Guess you like

Origin www.cnblogs.com/jingjingweixiao/p/11074154.html