[27] Learning C # abstract class and the principle of opening and closing

What is SOLID?

SOLID [5] acronym is object-oriented design principles, they are:

(1) Single Responsibility Principle (SRP) - Single Responsibility Principle

(2) Open Close Principle (OCP) - Open Closed Principle

(3) Liskov Subsititution Principle (LSP) - Richter substitution principle

(4) Interface Segreguation Principle (ISP) - Interface Segregation Principle

(5) Dependency Inversion Principle (DIP) - Dependency Inversion Principle

The five design principles bred dozens of design patterns, design patterns are more fixed and more high-end usage based on the basic principles

For making the base class [] [born abstract class]

  • Abstract methods : the "abstract" as modified, only the return value, the method name and parameter list, but there is no body of methods, the method is abstract

If a class contains abstract members, then this class will become an abstract class, it must be 'abstract "modified, otherwise the compiler will complain

//抽象类
abstract class Vehicle
{
	abstract public void Run();  //抽象方法
}
  • Therefore, the definition of the abstract class do the following:
    abstract class is:Function has not been fully implemented class members; That is to say, the abstract class can have several function members, but at least one member function, is (are "abstract" modification) is not implemented

  • Moreover, the function of the abstract class members are not implemented, its access level can not be "Private" , because in the child class that you want to achieve

  • The compiler will not instantiate an abstract class , it can be said:An abstract class is to make the base class is bornAnd its role is:
    (1) as a base class to implement a function member is not implemented in the derived class
    (2) refer to an instance of a derived class with the type of base type variable (polymorphic)

  • The only abstract class can do, it is to give another class when the base class to a base class type to declare a variable, and cited examples of those who have fully realized its abstract members of the sub-class type, multi-state performance

Open Closed Principle

[If not for bug fix or add features, idle all right do not grow old modify the code of a class, in particular, it is a function of a member of the class among the code -We should encapsulate the constant, stable, fixed, certain members, while those uncertainties, it is possible to change the members declared as abstract members, and left the child class to implement

Application of abstract classes and the principle of opening and closing

Note how the code is a step by step reconstruction
(1) can not be called to the variable v Run () method, because this method is not defined in the Vehicle class
Here Insert Picture Description

(2) bad solution
Here Insert Picture Description

(3) may be rewritten using
Here Insert Picture Description

(4) more preferably written to the abstract class
Note:When the abstract parent class does not implement the subclass implementation, to add "override"
Here Insert Picture Description

What is the interface?

There is no function for all members of an abstract class are abstract members?
Of course, there is
Here Insert Picture Description
a pure abstract class / pure virtual class, in fact, the interface
Here Insert Picture Description

Summary interfaces and abstract classes

Here Insert Picture Description

Published 29 original articles · won praise 3 · Views 929

Guess you like

Origin blog.csdn.net/weixin_44813932/article/details/104077129