Interface and abstract class

xiaoxiao2021-03-06  39

Interfaces and abstract classes allow you to create a definition of component interaction, through an interface, you can specify how components must implement, but do not actually specify how to implement it. Abstract classes allow you to create behaviors, and provide some public implementations for inherited classes, and tools for interfaces and abstractions in terms of component implementation polymorphism. Since mentioning the polymorphism of the component, you will briefly introduce, but this article does not focus on the multi-state introduction. C programming language author BJame Stroustrup believes that a language must support objects, classes, and inheritance these three concepts can be called the language of the object, but in fact, the language of the object is more commonly considered to be built in packaging, inheritance and Multi-state these three, visible polymorphisms are importance in object-oriented languages. The definition of polymorphisms on the MSDN is that the polymorphism is a class as a method (these methods are called by the same name) to provide different implementations. The polymorphism allows a method of calling a certain method without considering the particular implementation provided by the method. In fact, I am more concise understanding that allows the old code to call the new code. In fact, this is also the biggest benefit of object-oriented design, that is, you don't need to modify and destroy the original code structure, you can expand and enhance your original code. It seems a bit incredible for beginners. But if a system design is full, he can do this. There are several ways to implement the polymorphism of the component: 1. Polymority of the interface: The interface is essentially the definition of how the class responds. 2. Inherited Polymorphism: By inheritance, the class is received in the base class so attribute methods and events. 3. Realize polymorphisms by abstract classes: the abstract class also provides elements of inheritance and interface. The abstract class itself cannot be instantiated, it must be inherited. Some or all members of this class may not be implemented, and this implementation is provided by inheritance class. The implemented members can still be rewritten, and inherited classes can still achieve additional interfaces or other functions for the achievement of polymorphism, Microsoft is advised to recommend: "For small-scale development tasks, this is a powerful mechanism. However, for large-scale systems, there is often a problem that there is a problem, and the polymorphism of the inheritance of the driver generally leads to large-scale output steering design, which is not helpful to shorten the development time of the entire project. "

So how do you choose a design type in a specific case? 1. If you are expected to create multiple versions of the component, create an abstract class, an abstract class provides a simple and easy way to control the component version. By updating the base class, all inheritance classes are updated synchronously, and the interface cannot be changed once the interface is defined. If you need a new version of the interface, you must create a new interface. 2. If the created feature is used between a wide range of objects, you need to use the interface, the abstract class is mainly used for close objects, and the interface is best suited to provide unhesed classes. 3. If you design a small and concise function block, use the interface. If a large function unit is designed, an abstract class is used. 4. If you want to deliver universal implementation between all implementations of the component, use an abstract class. Abstract class allows the part to implement classes, and the interface does not contain any member's implementation.

转载请注明原文地址:https://www.9cbs.com/read-64347.html

New Post(0)