C # class, interface, structure, abstract class introduction and comparison of abstract and interface,
Classes in c # are the most common ones. In fact, they define a prototype of variables and methods for a certain type of objects.
The structure is the value type, and the class is the reference type.
1. Unlike classes, the new operator can be used for structure instantiation. The structure can declare constructors, but they must contain parameters.
2. A structure cannot inherit from another structure or class, and cannot be used as the base of a class. All structures are directly inherited from System. ValueType, and the latter inherits System. Object.
3. The structure can implement interfaces. The structure cannot specify an initial value when defining a variable. If you want to use constructors in the structure, you must assign values to all variables.
The interface is of reference type. It is similar to a class and has three similarities with the abstract class:
1. It cannot be instantiated;
2. Contains the unimplemented method declaration;
3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members );
In addition, interfaces have the following features:
In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).
Abstract classes are special classes, but they cannot be instantiated. In addition, they have other characteristics of the class. It is important that abstract classes can include abstract methods, which are not supported by common classes. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them.
Differences between abstract classes and interfaces:
1. class is the abstraction of objects. abstract classes can be understood as classes as objects. abstract classes are called abstract classes. the interface is just a behavior specification or provision. Microsoft's custom interface always carries the able field behind it to prove that it represents a class "I can do it... ". Abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions;
2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
3. A class can implement several interfaces at a time, but only one parent class can be extended;
4. interfaces can be used to support callback, but inheritance does not;
5. abstract classes cannot be sealed;
6. The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, you can also declare them virtual;
7. Similar to a non-abstract class, an abstract class must provide its own implementation for all the members of the interface listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods;
8. abstract classes implement a principle in oop that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented;
9. A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution;
10. Avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined. (Based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example, in asp.net, the Page class has Server Request and other attributes, but in fact they are all objects of a certain class. This object of the Page class is used to call the methods and attributes of other classes. This is a very basic design principle;
11. If an abstract class implements an interface, you can map the methods in the interface to the abstract class as an abstract method without having to implement it. Instead, you can implement the methods in the subclass of the abstract class.
Use of abstract classes and interfaces:
1. If you want to create multiple versions of a component, create an abstract class. Abstract classes provide simple methods to control component versions;
2. If the created function is used across a wide range of different objects, the interface is used. If you want to design small and concise functional blocks, use interfaces;
3. If you want to design a large functional unit, use an abstract class. If you want to provide general implemented functions among all the implementations of the component, use the abstract class;
4. abstract classes are mainly used for closely related objects. interfaces are suitable for providing general functions for irrelevant classes.