Preface :
Recent research .NET Core Some of the class library source code found in the underlying class library used a large number of abstract classes , However, it seems that I have never used abstract classes or abstract methods to implement a function module in a project for so many years , And for modifiers Abstract The concept only knows a little bit ( That is to say, during the interview, they will ask about their basic functions ). Of course , If we don't understand, we have to learn to understand , We can't let the technology and knowledge that we don't understand all the time . Next, let's explore C# Abstract The function of the modifier , And understand in what scenarios we will use abstract classes , To improve the robustness of our project .
abstract Modifier :
Basic concepts :abstract Modifiers indicate that there is a missing or incomplete implementation of something to be modified .
The range of embellishments :abstract Modifiers can be used to modify classes 、 Method 、 attribute 、 Indexer (indexer) And events .
abstract class :
public abstract class HttpRequest{}
Abstract method :
//todo: Abstract methods do not provide the actual implementation , So there is no method body ( And abstract methods can only be declared in abstract classes ) public abstract void ActionMethod();
Abstract properties :
public abstract string ContentType { get; set; }
Abstract indexer :
public string this[int index] => $" obtain _{index}";
Abstract Events :
// First declare the delegate type of the event public delegate void BoilerLogHandler(string status); // Define events based on the above delegation public abstract event BoilerLogHandler BoilerEventLog;
The similarities and differences between abstract classes and interfaces :
The use of abstract classes :
Is a common base class that can be shared by multiple derived classes .
The purpose of the interface :
Interface provides the standard structure that derived classes should follow .
The basic characteristics of abstract classes :
1、 Abstract classes are special classes , Except that you can't instantiate ( Members of an abstract class can only be obtained by instantiating a derived class that inherits an abstract method ) Outside , Has other properties of the class ( Can inherit alone ).
2、 Abstract classes can define abstract methods , Abstract methods have no reality .
3、 A class that inherits an abstract class must have its abstract members ( Members include : Method , attribute , Indexer , event ) Rewrite all (orveride), Except for abstract classes .
The basic features of the interface :
1、 Interfaces cannot be instantiated directly .
2、 A class or structure can implement multiple interfaces .
3、 Any class or structure that implements an interface must implement all its members .
4、 The interface contains only methods , attribute , Indexer ( It has a parameter property ), Four members of the event .
5、 Interface cannot contain instance fields 、 Instance constructor or Terminator .
6、 By default , Interface members are public , There can be no other modifiers .
The difference between abstract classes and interfaces :
identical :
1、 Can be inherited
2、 Can't be instantiated directly
3、 Both can contain method declarations and none of them implement
4、 Derived classes must implement unimplemented members
Difference :
1、 Interfaces can be implemented in multiple ways , Abstract classes can only be inherited by a single .
2、 Interfaces can be used to support callbacks , Inheritance does not have this characteristic .
3、 Abstract classes can define fields 、 Method 、 attribute 、 event 、 Implementation of indexer . Interfaces can only define properties 、 Indexer 、 event 、 And method statements , Cannot contain fields .
4、 The member access type in the interface is public by default , No other access modifiers can be used to modify .
5、 The keywords defined are different , Abstract classes need to use abstract, The interface uses interface.
The use of abstract classes and interfaces :
1、 Abstract class table commonness ( Often used in the same behavior and characteristics ), The interface is a table specification ( Used to define a behavior ).
2、 Abstract classes are mainly used for closely related objects , The interface is most suitable for providing general functions for unrelated classes .
3、 If you want to design large functional units , Then use the abstract class ; If you want to design small and concise function blocks , Then use the interface .
4、 If you expect to create multiple versions of a component , Then create an abstract class . Once created, the interface cannot be changed . If you need a new version of the interface , You have to create a new interface .
5、 If you create a feature that will be used across a wide range of disparate objects , Then use the interface ; If you want to provide common implemented functions among all implementations of a component , Then use the abstract class .
6、 Analysis object , Abstracts the internal generality, forms the abstract class , Used to represent the nature of an object , namely “ What is it? ”. Interface is preferred when providing external calls or functions .
7、 A good interface definition should be functional , Not multifunctional , Otherwise, the interface will be polluted . If a class only implements one of the functions of the interface , You have to implement other methods in the interface , It's called interface pollution .
Examples are as follows :
Instance of a : Like iron doors, wooden doors are all doors ( abstract class ), You want a door. I can't give it ( Can't instantiate ), But I can give you a concrete iron or wooden door ( polymorphic ); And it can only be the door , You can't say it's a window ( Single inheritance ); A door can have a lock ( Interface ) There can also be doorbells ( Multiple implementation ). door ( abstract class ) Defines what you are , Interface ( lock ) It specifies what you can do ( An interface can only do one thing , You can't ask the lock to sound, can you ( Interface pollution )).
Example 2 : For example, there are many students in our class , These students have their own specialties , hobby , The style of dressing , So we can reduce the code redundancy and define a general student abstract class to describe the height of students , weight , full name , hobby , A common abstract class of related features and behaviors .
Examples of three : We need to create “ Dog ”、“ cat ”、“ fish ”、“ Horse ” These objects ( class ), We can say that they have some common attributes like the mouth 、 tail 、 weight 、 Color 、 Size and other common attributes (properties), But the shapes of their properties are different from each other ( Like the mouth ), under these circumstances , If we define the similar properties one by one, is it more complicated ? If the abstract class is not very convenient to them to inherit . Abstract classes also have a better place , Embodied in “ Homogeneity and dissimilarity ” It is essentially the same implementation form of different methods inheritance , For example, the dog above 、 cat 、 The breathing method of horses, etc. or the method of running speed is different , This is to define an abstract method , Is it convenient to let their own classes implement it .“ abstract ” This is the meaning of . Take something in common and package it , But it's not implemented. It's just inheritance .
summary :
Through the in-depth understanding of the abstract class, it is found that there are many problems that need to be considered in programming optimization , The choice of abstract classes and interfaces is a typical example . Often we will simply define the interface to use , However, the robustness and expansibility of later software are often ignored . In the future development, we need to think more about this problem . Finally, if there is anything in the article that needs to be supplemented or insufficient, I hope you can correct it , thank you .
Reference material :
https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members
https://blog.csdn.net/lizhenxiqnmlgb/article/details/82346478
https://kb.cnblogs.com/page/41836/