First of all, thanks to a senior anytao.cnblogs.com in the garden, he made a comparatively perfect summary of the abstract class and interface programming. The following summary is directly copy his exact words.
A. The same point cannot be directly instantiated, and it can be implemented by inheriting its abstract methods.
Are the technical basis for abstract programming and have realized many design patterns.
Two. Different points
Interfaces support multiple inheritance, and abstract classes cannot implement multiple inheritance.
An interface can only define abstract rules, and an abstract class may define a rule and possibly provide a member that has already been implemented.
An interface is a set of behavioral specifications; An abstract class is an incomplete class that focuses on the concept of a family.
An interface can be used to support callbacks; An abstract class cannot implement a callback because inheritance does not support it.
An interface contains only the signatures of methods, properties, indexers, events, but cannot define fields and methods that contain implementations; an abstract class can define fields, properties, and include
Method of implementation.
Interfaces can be used for value types and reference types, and abstract classes can be used only for reference types. For example, struct can inherit an interface and not inherit a class.
Through the same and different comparisons, we can only say that the interface and abstract classes, strengths, but no advantage. In the actual programming practice, we should depend on the specific circumstances to the discretion, but the following experience and accumulation, may give us some inspiration, in addition to some of my accumulation, many are derived from the classic, I believe that can withstand the test. So in the rules and occasions, we learn these classics, the most important thing is to apply, of course, I will opinion broad family smile, reader please continue.
Three. Rules and occasions
Keep in mind that one of the most important principles of object-oriented thinking is: interface-oriented programming.
With the help of interfaces and abstract classes, many of the ideas in 23 design patterns have been cleverly implemented, and I think the essence of this is simply this: oriented to abstract programming.
Abstract classes should be used primarily for closely related objects, while interfaces are best suited for providing common functionality for unrelated classes.
The interface focuses on the can-do relationship type, while the abstract class emphasizes the is-a relationship;
Interfaces define the behavior of objects, and abstract classes define the attributes of objects;
Interface definitions can use public, protected, internal, and private modifiers, but almost all interfaces are defined as public, and the reason is not
Say it more.
"Interface unchanged" is an important factor to consider. So, when you add an extension to an interface, you should add new interfaces instead of changing existing interfaces.
Try to design the interface as a functional block with a single function, taking the. NET Framework for example, IDisposable, IDisposable, IComparable,
IEquatable, IEnumerable, etc. all contain only one public method.
The uppercase "I" in front of the interface name is a convention, just as the field name follows the line, please adhere to these principles.
In an interface, all methods default to public.
If a version problem is expected, you can create an "abstract class." For example, to create a dog (DOG), a chicken (chicken) and a duck (Duck), then it should be
Consider the abstraction of the animal (Animal) to deal with the possible future of the wind horse cattle. Adding new members to an interface forces the requirement to modify all derived classes and
Recompile, so the issue of versioning is best implemented as an abstract class.
A non-abstract class that derives from an abstract class must include the actual implementation of all inherited abstract methods and abstract accessors.
You cannot use the New keyword or be sealed on an abstract class because an abstract class cannot be instantiated.
You cannot use static or virtual modifiers in abstract method declarations.