C # differences between the inheritance of virtual base classes and interfaces

Source: Internet
Author: User
Class
Define new data types and how these new data types operate on each other

Definition method:

    1. Class cat
    2. {
    3. }
    4. Class Cat: Object
    5. {
    6. }

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, seal class, and non-abstract class
Abstract: The modified class is incomplete, that is, the abstract class. It can only be used as the base class. In use, it cannot be directly instantiated,
You cannot use the new operator.
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.

    1. Class Cat: Animal
    2. {
    3. Public void CAT ()
    4. {
    5. Base. Eat ();
    6. }
    7. }

In the preceding example, base represents animal.
Note: base can only be used inside a class.

Interface
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.

Interfaces are essentially one type.
However, the distinction between Class and Class is still obvious-it cannot be instantiated.
Its essence lies in providing some common classes and being able to "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 achieveProgram.

You don't have to use the object viewpoint to unify the interface. In my sense, the inheritance simplifies the object structure and addsCodeReuse, 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?

Differences:
The class definition needs to be "implemented", and the interface definition only needs to be "declared ";
Class can generate an object, and the interface cannot;
The class can only be inherited from one root, and the interface can be inherited from multiple root;
Inheriting a class is equivalent to borrowing the functions it has implemented and implementing an interface, which is equivalent to making an external commitment;
......

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 is that 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. Changes in the implementation of the virtual base class may affect the derived class.
5. interfaces are more adaptable than inheritance.

Interface features;
It 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 are also abstract class definitions in the class. The difference between the abstract class and the interface is:
An abstract class is an incomplete class. The class contains abstract methods, attributes, and specific methods and attributes, which requires 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, they all reflect the use of an interface.
The most direct benefit is the OCP in the design principle (the open and closed principle). We use interfaces without worrying about their specific implementations, the detailed changes in implementation are irrelevant to the client (Class Using Interfaces ).
Yes, it is open to and extension. We can write another interface implementation to expand the current program without affecting the upper-layer usage, but the modification is closed, that is, we cannot modify the interface definition.
"Not Allowed" means the rule should not be done in principle!

Interface Usage-Implementation of polymorphism:

    1. Public interface ipolimorphism ()
    2. {
    3. Void say ();
    4. }

Create a class1.cs file, define two classes, and extend ipolimorfism:

    1. Using system;
    2. Using system. Windows. forms;
    3. Namespace northwind
    4. {
    5. Public class RED: ipolimorphism // method for implementing the interface
    6. {
    7. Public void say () // you do not need to specify the override keyword
    8. {
    9. MessageBox. Show ("red! ");
    10. }
    11. }
    12.  
    13. Public class Yello: ipolimorphism
    14. {
    15. Public void say () // you do not need to specify the override keyword
    16. {
    17. MessageBox. Show ("Yello! ");
    18. }
    19. }
    20.  
    21. }

Another mainfrm. CS file, at the application portal:

    1. using system;
    2. using system. Windows. forms;
    3. namespace northwind
    4. {
    5. public class mainfrm: system. Windows. Forms
    6. {
    7. [stathread]
    8. static void main ()
    9. {
    10. ipolimorphism Red = new red ();
    11. Red. Say ();
    12. ipolimorphism Yello = new Yello ();
    13. Yello. Say ();
    14. }
    15. }
    16. }

for example, an old man made a pastry and gave it to his sons. The old man used it, added some stuffing, wrapped it into a steamed stuffed bun, the second child uses this noodle cake, which is oily on the outside, sprinkled with
raw material, baked, and made into a local bread. What about laosan? It is done randomly and put a mess on it, baked and made into pizza. this is the relationship between the base class and the derived class. The base class provides the basic implementation, and the new
class is added to the base class, and eventually becomes the cake 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. The interface makes it easier to express the design and communicate with each other, and the interface does not implement code. Therefore, you must define the interface and then write the implementation code, 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, which is like drawing the building drawings for the construction workers.


Self: http://blog.ppku.com /? P = 58

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.