This is the interface in C,

Source: Internet
Author: User

This is the interface in C,

The interface is used to solve the problem that multi-inheritance is not allowed in C.

1. What is an interface?

I think the interface can be understood as a uniform name for a set of method declarations, but these methods do not provide any implementation.

Through interfaces, you can manage methods in a unified manner, avoiding repeated definitions of these methods in each type.

 

2. How to Use interfaces for programming

2.1 Interface Definition

Interface ICustomCompare

{

// Define a comparison method. All classes that inherit this interface must implement this method.

Int CompareTo (object other );

}

You cannot add any access modifiers to methods defined in the interface, because the methods in the interface are public by default. If the modifier is specified explicitly, a compile-time error occurs. You cannot use the static keyword for modification.

In addition to defining methods, APIs can also contain attributes, events, indexers, or any combination of these four types of members (including methods; however, the interface type cannot contain fields, Operator overloading, instance constructor, and destructor.

 

2.2 inheritance Interface

Public class Person: ICustomCompare

{

Int age;

Public int Age {get {return age;} set {age = value ;}}

// Interface Implementation Method

Public int CompareTo (object value)

{

If (value = null)

{

Return 1;

}

// Forcibly convert the Object type to the Person type

Person otherp = (Person) value;

// Compare the Age attribute of the current object with the Age attribute of the object to be compared

If (this. Age <otherp. Age)

{

Return-1;

}

If (this. Age> otherp. Age)

{

Return 1;

}

Return 0;

}

}

In the Person class above, the CompareTo method in the ICustomCompare interface is implemented.

The CompareTo method first checks whether the parameter object is null. If it is null, 1 is returned directly, indicating that the current object is larger than the input object.

If the object is not empty, it is forcibly converted to the Person type to compare the Age property. If the current object's Age property value is greater than the Age property value of the input object, 1 is returned, if-1 is returned, the Age attribute of the current object is smaller than that of the input object.

 

2.3 call methods in an Interface

Class Program

{

Static void Main (string [] args)

{

Person p1 = new Person ();

P1.Age = 18;

Person p2 = new Person ();

P2.Age = 19;

// Call methods in the interface to compare p1 and p2

If (p1.CompareTo (p2)> 0)

{

Console. WriteLine ("p1 is larger than p2 ");

}

Else if (p1.CompareTo (p2) <0)

{

Console. WriteLine ("p1 is smaller than p2 ");

}

Else

{

Console. WriteLine ("p1 is bigger than p2 ");

}

Console. ReadKey ();

}

}

 

3. display interface Implementation Method

In the above sample code, the implicit interface implementation method is used, that is, the CompareTo method in which the implementation interface is not specified in the implementation code.

Of course, there are also explicit interface implementation methods. In the process of class implementation interface, it is clear that the method in which the interface is implemented.

Interface IChineseGreeting

{

// Method declaration

Void SayHello ();

}

Interface IAmericanGreeting

{

// Method declaration

Void SayHello ();

}

// The Speaker class implements two interfaces.

Public class Speaker: IChineseGreeting, IAmericanGreeting

{

// Implement the implicit Interface

Public void SayHello ()

{

Console. WriteLine ("hello ");

}

}

The above Speaker class implements two interfaces. it happens that the methods declared in the two interfaces have the same return type, the same method name, and the same parameters. If you use an implicit interface implementation method, the following code calls the same SayHello method.

Static void Main (string [] args)

{

Speaker speaker = new Speaker ();

// Call the Chinese greeting Method

IChineseGreeting chinese = (IChineseGreeting) speaker;

Chinese. SayHello ();

 

// Call the American greeting Method

IAmericanGreeting American = (IAmericanGreeting) speaker;

American. SayHello ();

}

When the Main function is executed, the interface is two "hello ";

This is not the expected result, so in this case, we must use an explicit interface implementation method to solve this naming conflict problem.

// The Speaker class implements two interfaces.

Public class Speaker: IChineseGreeting, IAmericanGreeting

{

// Explicit interface implementation

Void IChineseGreeting. SayHello ()

{

Console. WriteLine ("hello ");

}

  

// Explicit interface implementation

Void IAmericanGreeting. SayHello ()

{

Console. WriteLine ("Hello ");

}

}

This method can solve the naming conflict problem. When using the display interface Shanxi ian, pay attention to the following issues:

  • If the interface is implemented explicitly, the method cannot use any access modifier, And the explicitly implemented members are private by default.
  • Explicitly implemented members are private by default, so these Members cannot be accessed through class objects. The correct call method is to explicitly convert the speaker object to the corresponding interface and call the SayHello method through the interface.

For the above two methods: explicit implementation and implicit implementation, the following summarizes the differences between the two methods and their use scenarios:

  • When an implicit interface is used for implementation, both classes and interfaces can access methods in the interface. If an explicit interface is used for implementation, the interface method can only be accessed through interfaces, therefore, the interface method is private by default.
  • When a class implements a single interface, it usually uses the implicit interface implementation method, so that the class object can directly access the interface method.
  • When the class implements multiple interfaces and the interface contains the same method name, parameters, and return type, the explicit interface implementation method should be used. Even if there is no same method signature, explicit methods are recommended when multiple interfaces are implemented, because this can identify which method belongs to which interface.

 

4. interfaces and abstract classes

The differences between the two are described as follows:

  • Abstract classes are defined using abstract keywords, while interfaces are defined using interfaces. They cannot be instantiated.
  • Abstract classes can contain virtual methods, non-abstract methods, and static members. However, interfaces cannot contain Virtual Methods and any static members. In addition, interfaces can only define methods, but cannot implement them, the specific implementation of the method is completed by the Implementation class.
  • Abstract classes cannot implement multi-inheritance, while interfaces support multi-inheritance. Note: strictly speaking, class contact interfaces should be class implementation interfaces.
  • An abstract class is an abstraction of a class of objects, inherited from the relationship between the class of the abstract class and the abstract class. The class implementation interface only represents the method of implementing the class with interface declaration, is a Can-DO relationship. Therefore, the interface generally carries the able field, indicating "I can do it.

The above are only the main methods for differences between interfaces and abstract classes. There are many differences between them. You can refer to other books or blogs.

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.