The difference between C # interfaces and abstract classes [Reserved]

      First, the abstract class:
      abstract class is a special class, but can not be instantiated; In addition, having the characteristics of other; important abstract class may include abstract methods, which is not a common class. Abstract method can be declared only in the abstract class, and does not contain any implementation, the derived class must override them. Further, the abstract class can be derived from an abstract class, an abstract method can override base class may not be covered, if not covered, it derived class must cover them.


       Second, the interface:
      the interface is a reference type, similar to a class, and three parallels are abstract classes:
       1, can not be instantiated;
       2, comprising a method declaration is not implemented;
       3, derived class must implementation method is not implemented, an abstract class is an abstract method, then all members of an interface (not only the method comprising the other members);

       In addition, the interface has the following features:
the interface may comprise in addition to the method, may further comprise properties, indexers, events, and these members are defined as publicly owned. In addition, without any other members, for example: Constant domain, constructors, destructors, static members. A class can inherit multiple interfaces directly, but only directly inherit a class (including abstract).


      Third, the difference between abstract classes and interfaces:
      1. The class is an abstract object, can be understood as an abstract class to class as an object, as an abstract class called abstract class and the interface specification or just a prescribed behavior, Microsoft. custom Interface always after field with able to prove that it is expressed in a class of "I can do ...." more abstract class is defined between a series of closely related classes, and interfaces, but most of the loose relationship the class implements a function of 
      2. Interface basically do not have any specific characteristics of inheritance, it can only promise the method invocation;     
      3. once a class can implement several interfaces, but can only extend one parent class     
      4. The interface may be used to support callbacks have not inherit this feature.     
      The abstract class can not be sealed.   
      6. DETAILED abstract class is implemented as a virtual default, but the interface of the interface class method implementation has a default non-virtual, of course, you can declare virtual. 
      7. (interface) with similar non-abstract class, an abstract class must also provide its own implementation for all members of the interfaces listed in the base class list of this class. However, the abstract class allows to map the abstract methods to interface methods.   
      8. abstract class implements a principle of the oop, the variable and non-variable separation. Abstract classes and interfaces is defined as immutable, but the seat of variable subclasses to achieve.   
      9. A good interface definition should be a specific functional, rather than a multi-functional, interfaces or cause pollution. If a class is simply a function that implements the interface, but had to implement other methods in the interface, the interface is called pollution.   
     10. avoid using inheritance to implement the function set, but the use of black box multiplexing, i.e., object composition. Because the increase in inheritance hierarchy, resulting in the most direct consequence is that when you call this taxon in a class, you have to load them all onto the stack! The consequences can be imagined. (Combination of understanding the principles of the stack). At the same time, like-minded friends may notice that Microsoft when building a class, often use a combination of the method of the object. For example in asp.net, Page class, there Server Request other attributes, but in fact they are the objects of a class. Use this object to call the Page class methods and properties of another class, this is a very basic design principles.   
     11. If the abstract class implements an interface, the interface can be mapped to the abstract methods as abstract classes without having to implement a method, the method implemented in the interface subclasses of the abstract class.
   
      Fourth, the use of abstract classes and interfaces:
      1. If you expect to create multiple versions of a component, you create an abstract class. Abstract class provides a simple way to control the component version.
      2. If the function was created to be used in a wide range between disparate objects, use the interface. If you want to design small and concise function block, use the interface.
      3. If the functional unit to be designed large, an abstract class is used. To provide common among all components to achieve functionality has been achieved, using an abstract class.   
      4. main abstract class for objects close; and the interface is adapted to provide generic functions unrelated to the class.


 Here is what I see on the Internet a few metaphor, really good, huh:
1. aircraft can fly, birds fly, they have inherited the same interface "fly"; but F22 aircraft belonging to an abstract class, pigeons belong Bird abstract class.
2. Like iron gate doors are doors (abstract class), you want a door I can not give (can not be instantiated), but I can give you a specific gates or doors (multi-state); and can only be door, you can not say that it is a window (single inheritance); a door can be locked (interfaces) can also doorbell (more than realized). Door (abstract class) defines what you are, an interface (lock) sets out what you can do (the best one interface can only do one thing, you can not ask locks can sound bar (Interface pollution)).

Reproduced in: https: //my.oschina.net/cjkall/blog/195909

Guess you like

Origin blog.csdn.net/weixin_34240657/article/details/91756190