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

Source: Internet
Author: User

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

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 to 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 that cannot be instantiated is to provide a common place for some classes, in addition, we 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? It depends on what you think is the essence of a class. If you get stuck with this argument too much, it is easy to fall into the battle of words, which is of little significance. 2. what are the differences and relationships between interfaces and classes? The main difference is that the class definition requires "Implementation" and the interface definition only requires "Declaration". The class can generate an object by instance, the interface is not allowed; 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 implementing an interface by borrowing the functions it has implemented, which is equivalent to making a commitment to the outside world ;...... Contact: class can "IMPLEMENT" Interface 3. whether an interface has class features depends on what you think is "class features", hehe, for example, "A is generated after compilation. the class file is not a feature ...... 4. Whether the interface is an Object or not. 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 is more adaptable than the use of inheritance interfaces; 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 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 it can implement multiple interfaces PS: In addition to standardizing a behavior, the actual function of an interface in a specific project is also very important, in the use of object-oriented design principles and design patterns, all reflect the benefits of using an interface. The most direct is the OCP (open and closed principle) in the design principles. We use interfaces, you do not need to care about the specific implementation. The detailed changes in the implementation are not related to the use of the client (the class using the interface) and are open to the extension, we can write another interface implementation to expand the current program without affecting the upper-layer usage. However, the modification is closed, that is, we cannot modify the interface definition, of course, this "cannot" means that this should not be done 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 interfaces do not implement code. Therefore, you can define an interface and then write the implementation code, even if there is a change in the design, 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.

Related Article

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.