Review solicitation Stickers: http://www.cnblogs.com/BeginnerClassroom/archive/2010/07/30/1788649.html
Appendix collection Stickers: http://www.cnblogs.com/BeginnerClassroom/archive/2010/08/04/1792175.html
You are welcome to expand a part of the content of this book, which will be attached to the book in the form of an appendix.
Requirements:
- Closely around one or two centers;
- Clear logic and smooth writing;
- Consider the basics of beginners.
- It is recommended that the writing time be less than one week.
After I write everything, I put it there first. After a while, I read it and modify it again. It is basically smooth after so many times.
(PS: it will be signed, but there is no draft fee, because there is not much, not enough points. Of course, if you make a fortune, I will give it to you .)
Title |
Author |
Status |
Research on modifying the font size of RichTextBox |
Li yilai |
Completed |
The idea of policy delay for delegation and interfaces |
Tang fan |
Completed |
XML format comment |
Zhang Zhiming |
Completed |
Differences between abstract classes and interfaces and Their Applications |
Zhang Yang |
Completed |
. Net version change history |
Zhang Zhiming |
Completed |
Character encoding |
Zhao Shijing |
Writing |
Stream Application Instance |
Huang zhibin |
Completed |
Sharpen C # capability through the online Evaluation Platform |
Cao rujin |
Completed |
Application of Regular Expressions in emeditor |
Liu yongfa |
Completed |
C #ProgramCode Specification |
Gu lei |
Completed |
Asynchronous read/write operations |
|
To be selected |
Control Development and custom control |
Minghao_hu |
Writing |
Relationships and differences between structures and Classes |
|
To be selected |
Drawing Cache |
|
To be selected |
Regular Expression application instance |
Air Force |
Writing |
(You are welcome to provide other appendices) |
|
|
Differences between abstract classes and interfaces and Their Applications
(This article is provided by Zhang Yang)
Abstract class and interface are two important concepts in object-oriented programming. Because the two have many similarities in their own features and application methods, such as neither can be instantiated nor can they be inherited (strictly speaking, interfaces should be called implementations, in many people's minds, the boundaries between abstract classes and interfaces are very vague. They are even more confused about when to use abstract classes and when to use interfaces.
The purpose of this article is to help readers identify the differences between abstract classes and interfaces in their ideological nature and application scenarios through discussion and comparison. if this can be done, you can easily select and use abstract classes and interfaces based on the actual situation.
1. abstract classes and interfaces are object-oriented concepts, not programming languages.
If you want to understand abstract classes and interfaces correctly, you must first understand that these two concepts belong to the object-oriented ideology level, not a programming language. For example, the language element declared using the interface keyword in C # is called "interface". In fact, this is inaccurate, this should be called "implementation mechanism of interfaces in C # language ".
Object-oriented thinking involves many concepts, and different object-oriented languages have different implementation mechanisms for these concepts. For example, C ++ does not have a keyword corresponding to the interface in C #. Is there no interface concept in C ++? None! In C ++, if you want to define an interface, you can define all methods in a class as pure virtual methods [①.
Here we can see that it is also an interface, which is defined by the interface keyword in C #, and C ++ defines it by creating a class that only contains pure virtual methods, this means that the same concept has different implementation mechanisms in different languages. Similarly, the abstract keyword is not used in C ++ to define an abstract class. However, if a class contains at least one pure virtual Method and Its methods are not all pure virtual methods, this class is called an abstract class.
From the above analysis, we can see that it is impossible to accurately understand the true meaning of the abstract classes and interfaces at the language level, because different languages have very different implementation mechanisms for the same concept. If a C # beginner simply understands the two as "the class modified with abstract is an abstract class, and the language element defined with interface is an interface ", then he will be confused when he comes into contact with C ++, because there is neither abstract nor interface in C ++, the pure virtual method in the class determines whether it is a class, an abstract class, or an interface.
By clarifying the above problems, we can give a real definition of abstract classes and interfaces.
Abstract classes cannot be instantiated,However, the methods can include specific implementations.Code.
An interface is a set of method declarations,Only the method declaration should be included, and no implementation code is allowed.
The definitions of abstract classes and interfaces are independent of any specific language, but defined from the perspective of Object-oriented Thinking. Different languages can have different implementation mechanisms.
From the above definition, we can find a major ideological difference between the two:Abstract class is a class (Class), The interface is a set (Set)The two are not in essence. This is the first difference we have summarized. Ask the readers to put the bold words on the top for ten times. The higher the sound, the better. But if the roommate or neighbors throw eggs, please do not contact me.
2. abstract classes are the abstraction of ontology and interfaces are the abstraction of behavior.
Before starting this section, I would like to ask you a question, "I am a person" and "I can breathe" express the relationship between "I" and "people" and "I" and "breathing" respectively, are these two statements a relation? If you can easily distinguish between an abstract class and an interface, which means "yes" and "yes", congratulations.
When reading this section, please remember the above question and the following sentence:
Abstract class Representation"Is (IS-A)"Link abstraction, interface Representation"Energy (Can-do)"Link abstraction.
Read the above aloud for 10 times as usual.
Okay. Please clean the eggs on your head. Let's continue.
From the bold Chinese text above, we can see that abstract classes and interfaces have a common feature-they are all "an abstraction of a certain relationship", but the types are different. In fact, it is correct to change the "abstract class" in the first half of the above sentence to "class". It is not surprising that the abstract class is just a special class.
Next we will first explain the IS-A relationship. In fact, the IS-A relationship in English can be interpreted as two situations in Chinese, when the IS-A is used between an object and a class, it means "this object is an instance of the class ", for example, Guan Yu is an object, we can say "guanyu IS-A General", where General (General) is a class, which indicates that Guan Yu is an instance of the general class. And when IS-A is used between two classes, I think it is called IS-A-KIND-OF more accurately, indicating that "is a" in Chinese, such as "General IS-A person ", indicates that the general class is a type of human class. In contrast, the object-oriented terminology can be described as follows: General is a sub type of person ), person is the parent class or super type of general, and general inherits from person.
This latter IS-A relation is the relation expressed by the abstract class. From the analysis, we can see that the relationship expressed by abstract classes is actually one of the three main characteristics of object-oriented-inheritance, that is, the relationship expressed by abstract classes, there is no difference between the class and the general class, but the abstract class is no different from the general class except that it cannot be instantiated. Abstract classes are generated because some methods (often pure virtual methods) cannot be implemented at a high abstraction level and must be implemented by their subclasses according to their respective situations. Because it contains pure virtual methods, it is unreasonable to instantiate these classes, but we hope to abstract some of these subclasses to reduce code duplication, so the abstract class -- it contains reusable parts, but cannot be instantiated.
Therefore,The use of abstract classes is motivated to reuse code without the restriction of Instantiation.Keep this motivation in mind.
Then let's talk about the relationship between interfaces and can-do.
We know that the basic idea of object-oriented programming is to implement the functions of the program through the collaboration between objects. Specifically, in Object-Oriented Programming, each class is required to hide internal details (encapsulation) and only expose a group of public methods, objects call each other's common methods to complete program functions.
As you can see, in the object-oriented thinking, there is no need to know between objects. The caller may even have no idea who the caller is, as long as he knows what the called can do. This is like calling 110 for an alarm. You don't know what the other party looks like, what clothes to wear, whether you get married, whether you have children, or where the other party is, who the target person is, however, you know that the other party will be able to "Receive police", so you can complete the alarm smoothly.
What can we do is the can-do relationship. When we abstract the can-do relationship to form a set of can-do relationships, this is the interface. What are the motivations for using interfaces?One of the motivations is loose coupling.We know that "low coupling" is an important principle in object-oriented programming, and a large part of coupling is the call relationship, which is called "dependency" in object-oriented programming ". If there is no interface, the caller must rely on the called user. Just like in the Age of no 110 Alarm, you only know one police officer and do not know the calls of other police officers, when you call the police, you must call the police officer. If the police officer takes a vacation or is ill one day, you will not be able to call the police unless you meet another police officer. At this moment, we say that you are dependent on this police officer, also called tight coupling. But it is different when there is an alarm of 110. We regard "access to police" as an interface, and there is a method "access to police" in the interface. After dialing 110, the person on the other end of the phone must have implemented this interface. At this time, the alert Contact no longer depends on the specific police officer, but on the "accessible police" interface, which is called loose dependency.
So,The interface can also be seen as a set of rules, which is a guarantee for the caller and a constraint on the caller.In the preceding example, the alert contact personnel (callers) can be alerted to ensure that the caller can access the alert. At the same time, the alert contact Department must arrange a person who has implemented this interface in front of the alert contact phone. Even if this is a robot or a cleaning worker who has just been trained for two hours, it doesn't matter.
Another motivation for using interfaces is to implement polymorphism.[②].
Imagine that you were assigned to a new R & D team as the supervisor. On the morning of the first day of work, a group of people stood in front of you and waited for your instruction. You didn't know them at all, I don't know their respective roles, but you can say "go to work", so we are all birds and birds, programmers write programs, accountants Check Accounts, salesmen go out to contact customers ...... When you do this, you use the interface to implement polymorphism. As you know, they all implement the "work-ready" interface. Although the specific implementation of "work" varies, it doesn't matter, you just need to call their "work" method and they will do their own thing. If you cannot use polymorphism for interfaces, you need to say one by one: "programmers write programs, accounting staff audit accounts, and sales staff are about to contact customers ......", This is really difficult.
Make a summary of the content in this section:
Abstract class Representation"Is (IS-A)"Abstract: It abstracts the class ontology, and its motivation is to reuse code without the restriction of Instantiation. Interface Representation"Energy (Can-do)"Abstract relationship, which abstracts the behavior of classes. Its motivation is loose coupling between objects and program polymorphism.
Okay, I'll read it ten times as usual, but this time I'll allow you to meditate, because I'm afraid it's not an egg but a brick.
After the above analysis, I think you can easily choose between abstract classes and interfaces. If you want to extract the public code of a series of classes, reduce code duplication, and these classes and abstract classes can be expressed as IS-A relationships, use abstract classes; if you want to abstract one or a group of behaviors for loose coupling between objects or for polymorphism, use interfaces.
3. Discussion of abstract classes and interfaces in C #
In this section, we will discuss the differences among C # languages that are well known:InC #A class can inherit only one abstract class, but multiple interfaces can be implemented.
If you fully understand that the abstract class corresponds to the IS-A and the interface corresponds to the can-do, this constraint is not surprising. Logically, a class can only "be one" of all classes with the same abstraction level, but can "do multiple things ". The same abstraction layer here refers to a complete set of non-inheritance relationships between each other.
For example, {swine, ox, dog, and CAT} can be viewed as having the same abstract level. A lower-level class can only be a subclass of one of them. A class cannot be both a subclass of cattle and a subclass of pigs, however, it is possible that both the subclass of cattle and the subclass of animals, such as cows, because "Animals" and "cows" are not at an abstract level, "Ox" is a subclass of "animal.
Generally, ifClassaYesClassbIs also a subclassClasscMust exist.ClassbYesClasscSubclass orClasscYesClassb.
In other words, a class inherits two classes that have no inheritance relationship with each other logically. This explains why C # cannot inherit more than one abstract class at the same time. If a class inherits two abstract classes, there must be an inheritance relationship between the two abstract classes logically. Therefore, you only need to let the class inherit the specific abstract class. For example, the original design is "cows" that inherit "cows" and "Animals" at the same time, but it is easy to find that "cows" and "Animals" already have inheritance relationships, "Ox" is inherited from "Animals". Therefore, you can change the inheritance relationship to "cow" and only inherit "Ox", so that "Ox" inherits from "animal ", this eliminates multiple inheritance.
The can-do relationship of interfaces does not have such conflicts logically. Therefore, C # allows multiple interfaces to be implemented. The readers should think about the specific reasons.
By the way, C ++ allows many re-inheritance because C ++ does not implement non-abstract classes, abstract classes, and interfaces using classes, but does not differentiate them into different language elements at the language level, in fact, if the design is good, there should be no multi-inheritance of abstract classes. C # imposes constraints on the language level, which is more conducive to good design, c ++ is flexible in this regard and requires developers to grasp it. Therefore, it is more difficult for beginners to grasp abstract classes and methods.
Zhang Yang
2010.08.28
Leoo2sk.cnblogs.com
[①] Pure Virtual Methods in C ++ refer to methods without implementation code, which correspond to methods with abstract modification in C.
[②] Although abstract classes are sometimes used to achieve polymorphism, the motivation is not as direct as using interfaces at all. I personally agree that the method that requires polymorphism is abstracted as an interface.