C # differences between the inheritance of virtual base classes and interfaces
Source: Internet
Author: User
This article from: http://blog.ppku.com /? P = 58
Class defines the new data types and the method of mutual operations of these new data types:
Class cat
{
}
Class Cat: Object
{
}
C # All classes are derived from the object class by default. The results indicated or omitted are the same, so the two examples above are identical.
C # classes include: abstract class, sealed class, and non-abstract class Abstract: indicates that the modified class is incomplete, that is, the abstract class, which can only be used as the base class. The new operator cannot be directly instantiated during use. Sealed: indicates that the modified class cannot be derived, that is, the sealing class.
Base: access the nearest base class, that is, the class inherited by the current class.
Class Cat: Animal
{
Public void CAT ()
{
Base. Eat ();
}
}
In the preceding example, base represents animal. Note: base can only be used inside a class.
An interface defines a protocol. Interfaces can be considered as special abstract classes, but they are different. An interface can be inherited from multiple basic interfaces, while a class or structure can implement multiple interfaces. The interface can contain methods, attributes, events, and indexers. The interface itself does not provide the Implementation of the defined members, but the abstract class can provide the Implementation of the defined members. The interface only specifies the class or structure required to implement this interface.
An interface is essentially a type, but the distinction between it and the class is very obvious. The essence of an interface cannot be instantiated is that it provides some common areas of the class and can "inherit more"
We know that the so-called object is actually the encapsulation of data and methods. The function of an interface is more engineering. Generally, we use it to encapsulate common behaviors to achieve program polymorphism.
You don't have to use the object viewpoint to unify the interface. In my feeling, inherit and simplify the object structure, increase code reuse, and abstract interface behavior.
1. is an interface essentially a class?
This is because you think that "what is the essence of the class" is too entangled in this statement, and it is easy to fall into the competition of words, which is of little significance.
2. What are the differences and connections between interfaces and classes?
Difference: the class definition needs to be "implemented", and the interface definition only needs to be "declared"; the class can produce an object for instance, and the interface cannot; the class can only be inherited by a single root, and the interface can be inherited by multiple root; inheriting a class is equivalent to borrowing the functions it has implemented and implementing an interface, which is equivalent to making a commitment to the outside world ;......
Contact: class can be "implemented" Interface
3. Whether the interface has the characteristics of the class
This depends on what you think is "class features", hehe, for example, "a. Class file will be generated after compilation" is not a feature ......
4. Whether the interface is an object
No. However, you can use an interface variable to reference an object. Besides, the referenced object must be an object.
1. What is different between pure virtual classes and interfaces, pure virtual classes can contain some implementations. 2. Use pure virtual classes to include some basic functions or methods. 3. Use interfaces to define communication methods between clients and components. 4. Implement Virtual base classes. the changes may affect the derived class. 5. The use of interfaces has stronger applicability than the use of inheritance.
Some features of the interface; cannot contain any implementations; no constructor; any member is implicitly declared as public;
The interface is responsible for defining functions. The project uses interfaces to define the concepts of classes, Operation classes, and abstract classes! The class is responsible for the specific implementation of the function!
There is also an abstract class definition in the class. The difference between the abstract class and the interface is that the abstract class is an incomplete class, which contains abstract methods and attributes, you can also have specific methods and attributes that require further specialization. But the interface is a behavior specification, and everything in it is abstract! A class can only inherit one base class, that is, the parent class, but multiple interfaces can be implemented.
PS: In addition to standardizing a behavior, interfaces play an important role in a specific project. In the use of object-oriented design principles and design patterns, all of them reflect the benefits of using an interface. The most direct thing is the OCP (open and closed principle) in the design principle. We use interfaces without worrying about their specific implementations, the detailed changes in implementation have nothing to do with the use of the client (the class using the interface). It is open to extensions. We can also write an interface implementation to expand the current program, it does not affect the upper-layer usage, but it is closed to the modification. That is, we cannot modify the interface definition. Of course, this "cannot" means that we should not do this in principle!
Interface Usage-Implementation of polymorphism:
Public interface ipolimorphism ()
{
Void say ();
}
Create a class1.cs file, define two classes, and extend ipolimorfism:
Using system;
Using system. Windows. forms;
Namespace northwind
{
Public class RED: ipolimorphism // method for implementing the interface
{
Public void say () // you do not need to specify the override keyword
{
MessageBox. Show ("red! ");
}
}
Public class Yello: ipolimorphism
{
Public void say () // you do not need to specify the override keyword
{
MessageBox. Show ("Yello! ");
}
}
}
Another mainfrm. CS file, at the application portal:
Using system;
Using system. Windows. forms;
Namespace northwind
{
Public class mainfrm: system. Windows. Forms
{
[Stathread]
Static void main ()
{
Ipolimorphism Red = new red ();
Red. Say ();
Ipolimorphism Yello = new Yello ();
Yello. Say ();
}
}
}
For example, an old man made a noodle cake and gave it to his sons. The old man used it, added some stuffing and wrapped it into a steamed stuffed bun, the second guy used this cake to put oil on the outside, spread the food, bake it, and make it into a home slice. The old three made it randomly. Put a mess on the slice and bake it, it is made into a pizza. this is the relationship between the base class and the derived class. The base class provides the basic implementation, and the derived class is added to the above, and eventually becomes the pie that everyone wants. the story of the interface is different. The old man did not give everyone a picture. The old man drew a pie Design for his son, specifying the thickness and diameter of the cake, then, I sent the spec sheet to my son. The son made the pie chart based on their own ideas. the difference between the two stories tells us that an interface is a type and a unified standard. In actual application, an interface has two functions. 1. It is easier to design and communicate through interfaces, and the interface does not implement code. Therefore, if the interface is set and the implementation code is written, even if there are design changes, it can also be easily modified through the interface. if you do not need an interface, you will directly write the class to implement the code of a class. If there is a slight change in the design, you need to modify the code. 2. the interface is visible to the implementer of the writing class. This is like drawing the building drawings for the construction workers.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.