Thought 1: the differences between interfaces and abstract classes in C:
--------------- YYC
1. abstract classes cannot be instantiated, but they can have their own member fields and non-abstract methods. However, interfaces are more abstract than abstract classes, and their members can only be
Common methods, attributes, and index functions do not have their own member fields and constructors.
2. Methods in abstract classes must have access modifiers such as public keywords.
Methods must be abstract declared as abstract methods or virtual declared as virtual methods (except non-abstract METHODS, virtual methods have simple execution
Line code. You can choose not to overload the derived class. The abstract method does not execute any code, and must be overloaded in the derived class ). In its derived class
Override must be used to override an abstract or virtual method. In the interface class declared with the interface
1. static files are not allowed, followed by no code execution.
You cannot add any access restriction modifiers and do not use virtual or abstract modifiers. At the same time, it cannot be used in the type that directly supports its interface.
The override modifier is used to represent the overloaded method. All methods in this interface must be implemented. However, you can use the new keyword to hide methods in the parent interface.
3. interfaces, abstract classes, and non-abstract classes can all inherit interfaces.
4. Implement the Interface Method After inheriting the interface:
Implementation can be divided into "display implementation" and "implicit implementation"
Interface Interface1
{
Int GetInt ();
// The derived class must implement every method in the interface class
}
Class cl1: Interface1
{
Public int GetInt () // implicit implementation
{
Return 1;
}
}
Class cl2: Interface1
{
Public int GetInt () // implicit implementation
{
Return 2;
}
Int Interface1.GetInt () // display implementation
{
Return 4;
}
}
The difference between the two is that the implicit implementation method can be called either through an interface object or through an instance of a derived class.
The display implementation method can only be called through the interface object. In addition, no access is allowed before this method.
Modifier, which is implicit by default, so the object of the derived class cannot call this method.
Question 2: What is the meaning of an interface in C.
I. solved the problem that polymorphism and dynamic hiding cannot be unified.
A. Dynamic hiding: The new Keyword is used to hide the methods of the base class without interface mechanism.
The base class object and the derived class object call different methods by using the same method name. In the interface mechanism
The implicit implementation and display implementation of the interface method coexist to realize that the interface object and the derived class object call different methods by using the same method name.
For example: // dynamically hide the interface
Cl2 c2 = new cl2 ();
Interface1 iter = c2;
MessageBox. Show (c2.GetInt (); // output 2 ------ the implicit method is called.
MessageBox. Show (iter. GetInt (); // output 4 ------- the display method is called.
B. Implementation of polymorphism: in the absence of an interface mechanism, the implementation of polymorphism is achieved through the use of the keyword abstract/virtual -- override overload.
While the polymorphism in the interface mechanism can be achieved through multiple implicit implementations through display.
For example: // interface Polymorphism
Cl1 c1 = new cl1 ();
Cl2 c2 = new cl2 ();
Interface1 iter = c1;
MessageBox. Show (iter. GetInt (); // output 1
Interface1 iter = c2;
MessageBox. Show (iter. GetInt (); // output 4 -------- the display method is called. If the display method is not displayed, the implicit implementation method is called.
C: problem: the key problem is that the new mechanism and abstract/virtual -- override cannot appear at the same time in the mechanism without interfaces,
In the interface mechanism, you can easily solve this problem by displaying both implementation and implicit implementation methods. Dynamic under the C # Interface Mechanism
There are at least three modes for hiding and polymorphism at the same time:
For example:
/* Mode 1 :*/
Cl1 c1 = new cl1 ();
Cl2 c2 = new cl2 ();
Interface1 iter = c1;
MessageBox. Show (iter. GetInt (); // output 1
Interface1 iter = c2;
MessageBox. Show (iter. GetInt (); // output 4 -------- the display method is called. If the display method is not displayed, the implicit implementation method is called.
MessageBox. Show (c2.GetInt (); // output 2
// Outputs 2 and 4 implement polymorphism in 1 and 4, while outputs 2 and 4 implement dynamic hiding.
/* Mode 2 :*/
New cl1 class
Class cl1: Interface1
{
Public virtual int GetInt () // implicit implementation
{
Return 1;
}
Int Interface1.GetInt () // display implementation
{
Return 3;
}
}
Class cc: cl1
{
Public override int GetInt ()
{
Return 5;
}
}
Class cd: cl1
{
Public override int GetInt ()
{
Return 6;
}
}
Cl1 c1 = new cl1 ();
Interface1 iter = c1;
// Dynamically hide
MessageBox. Show (iter. GetInt (); // output 3 --- display implementation
MessageBox. Show (c1.GetInt (); // output 1 -------- display implementation
Cc PC3 = new cc ();
Cd cd1 = new cd ();
// Polymorphism
C1 = maid;
MessageBox. Show (c1.GetInt (); // output 5
C2 = cd1;
MessageBox. Show (c1.GetInt (); // output 6
Therefore, using interfaces can easily solve the unified problem of polymorphism and dynamic hiding.
Ii. Implement Multi-Inheritance
Implementing Multi-inheritance is also one of the unique and important functions of interfaces in C,
Alas, this part will write another blog tomorrow. The above content is just my personal understanding. Please help me to point out that YYC should thank you again...
To be continued .....