Interface implementation polymorphism in C,

Source: Internet
Author: User

Interface implementation polymorphism in C,

We all know that virtual methods implement polymorphism and abstract methods implement polymorphism. Let's take a look at how to use interfaces to implement polymorphism.

1. First, we need to understand what interfaces are and how they exist.

01. An interface exists to constrain the format (parameter and return value type) of the method.

02. The interface can implement multi-inheritance to make up for the defect of single inheritance.

03. The interface can be considered as a special abstract class. We can see the source code through decompilation.

04. access modifier is not required for methods in the interface, because CLR will automatically add and there cannot be a method body

05. If a class implements an interface, all methods of the interface must be implemented.

06. Use the interface with caution to prevent interface contamination!

07. An interface represents only one capability. The class implementing this interface has no inheritance relationship with the interface.

08. interfaces are implemented and classes are inherited.

09. In fact, many times, it seems that interfaces are not needed, because interfaces are a method convention,

It indicates that you must have some methods for this class, but you can also use these methods without writing interfaces,

Interface variables can be used for unified calls to achieve Polymorphism

10. The interface can only define methods, but not variables.

2. Differences between abstract classes and interfaces:

When there is a parent-child relationship between objects, you can consider using abstract classes,

When each object does not have an inheritance relationship, but only has the same capability, you must consider using interfaces.

3. Common Understanding of interfaces

01. Planes fly and birds fly. They all inherit the same interface "fly", but F22 is an abstract class of aircraft,

Pigeons belong to the bird abstract class.

02. Just like doors and doors are all doors (abstract classes), you can't give me a door (it cannot be instantiated ),

But I can give you a specific iron door or wooden door (polymorphism); and it can only be a door, you cannot say it is a window (single inheritance );

A door can have a lock (Interface) or a doorbell (multiple implementations ).

A door (abstract class) defines what you are and an interface (LOCK) specifies what you can do.

(One interface is best to do only one thing. You can make a sound without a lock (interface pollution ))

 

The following is an example of using an interface to implement polymorphism:

01. Create a New flying Interface

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace interface {// fly interface public interface IFly {// access modifier is not required for methods in the interface, because CLR will automatically add, and there cannot be a method body void Say ();}}

02. Create an interface for eating

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace interface {// public interface IEat {// access modifier is not required for methods in the interface, because CLR will automatically add, and there cannot be a method body void eat ();}}

03. Create a bird to implement the flying and eating Interfaces

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace interface {// bird's flying interface and eating interface public class Grid: IFly, IEat // note interface: interface is called inheritance, class: interface implementation {// If a class implements an interface, you must implement all methods of this interface public void Say () {Console. writeLine ("bird flying");} public void eat () {Console. writeLine ("bird eating ");}}}

04. Create an aircraft class to implement the flying Interface

Using System; using System. collections. generic; using System. globalization; using System. linq; using System. text; using System. threading. tasks; namespace interface {// aircraft flying interface public class Plan: IFly {public void Say () {Console. writeLine ("Flying ");}}}

05. Implement the call in the Main method

 

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace interface {class Program {static void Main (string [] args) {// defines the IFly [] iFlies = {new Grid (), new Plan ()}; // The cyclic array call method to implement multi-state foreach (IFly iFly in iFlies) {iFly. say () ;}// the bird eats the instantiated object IEat iEats = new Grid (); // call the method to implement the polymorphism iEats. eat (); Console. readLine ();}}}

The running result is as follows:

 

Note: If a class needs to implement two interfaces, it is unfortunate that the two interfaces (for example, IFly and IPlay) have the same names as the two methods (Eat) on the right)

We all know

If a class implements an interface, all methods of the interface must be implemented.

What should I do? Self-owned solutions:

We can use the interface name. method to implement

 

// Implements the IFly and IPlay interfaces, but both interfaces have the Eat method public class Dog: IFly, IPlay {// If a class implements an interface, you need to implement all the methods of this interface // so that we can use the interface name. method To implement void IFly. eat () {} void IPlay. eat (){}}

4. Interface Summary (excerpt ):

An interface is a protocol between components. It describes the external services provided by components. Technically speaking, interfaces are a set of data structures that contain functional methods. Through this set of data structures, the customer code can call the functions of component objects. Interfaces can be inherited from the parent interface. The inheritance of interfaces is descriptive inheritance first, rather than implement inheritance. Its implementation needs to be implemented through classes or structures. Secondly, interface inheritance can be multi-inheritance.

The essence of attributes is method, and the essence of Indexer is attribute.

The Interface contains methods, attributes, index indicators, and events. Because the interface allows multi-inheritance, full names can be used to avoid ambiguity. You can use classes to implement interfaces. The implementation of locating interface members in a class is called interface ing. The class must provide specific implementation for all interface members, including explicitly defined members in the interface and the members inherited from the interface from the parent interface. Similarly, explicit interface member execution bodies can be used to avoid ambiguity during interface implementation. A derived class can re-implement the interfaces implemented by the Base class.

 

Abstract classes can also implement interfaces, but interface members must be mapped to abstract members of abstract classes. If the derived class of an abstract class is not an abstract class, you must use method overloading to implement interface members.

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.