Abstract class Interface

Software is a product of a branch, interfaces and abstract classes are industrial software engineering.

definition

Specific Class -> abstract class -> Interface: more and more abstract, something inside achieve less and less.

Born base class for making abstract class


Member function is not fully implemented in the class to be abstract, keywords abstracted modification.
Examples of not allowed abstract class, the base class is doing an action (inherited), 2 is a reference to an instance of a subclass of the base type variable (derivative). Abstract subclasses implemented method, virtual functions similar to overwrite rewrite, also known as pure virtual method (c ++).

Open Closed Principle

To the effect that if not for the bug repair and modification capabilities, not to modify the class member function of the code package stable member functions, member of uncertainty packaged as abstract members for inheritance subclasses.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            Vehicle v=new Car();
            v.Run();//现在我们用Vehicle类型的变量去引用car类型的实例,是无法获取到方法的。

        }
    }

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


    }

    class Car:Vehicle
    {
        public void Run()
        {
            Console.WriteLine("car is running");
        }
    }
    
    class Truck:Vehicle
    {
        public void Run()
        {
            Console.WriteLine("Truck is running");
        }
    }
}
using System;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            Vehicle v=new Car();
            v.Run();

        }
    }

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

        public virtual void Run()
        {
            
        }

    }

    class Car:Vehicle
    {
        public override void Run()
        {
            Console.WriteLine("car is running");
        }
    }
    
    class Truck:Vehicle
    {
        public override void Run()
        {
            Console.WriteLine("Truck is running");
        }
    }
}

So the Vehicle class can become an abstract class, subclass of the same name to be added to override

    abstract  class Vehicle
    {
        public void Stop()
        {
            Console.WriteLine("Stopping");
        }

        public abstract  void Run();

    }

Decoupling is born Interface - fully unimplemented logic classes

Interface must be pure abstract class (member functions without specific content) and disclosed, the former will be omitted and public abstract class name and member functions, plus an interface only the class name, class name of the member function void +.

The difference between abstract classes and interfaces

An abstract class can have fields and non-public members who represent specific logic;
and the interface only member functions, and are all public, public implicit.
They can not be instantiated, can only be used to declare variables, specific reference to an instance of the class.

supplement

solid design patterns

Single responsibility principle
the principle of opening and closing
the Richter Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle

Design patterns learning needs skilled use of solid principles, design patterns are evolved from these basic principles.

Guess you like

Origin www.cnblogs.com/lpxspring/p/12436205.html