13th Chapter Interface

Source: Internet
Author: User
Tags modifiers

Multiple inheritance (multiple inheritance) refers to the ability of a class to derive from two or more base classes.

The CLR does not support multiple inheritance, and the CLR simply provides multi-inheritance of the "shrunk version" through the interface.

The class or struct that implements the interface must implement the interface members specified in the interface definition.

Interface iequatable<t>

{

BOOL Equals (T obj);

}

Any class or struct that implements the Iequatable<t> interface must contain the definition of the Equals method that matches the signature specified by the interface.

public class Car:iequatable<car>

{

public string make {get; set;}

public string Model {get; set;}

public string Year {get; set;}

public bool Equals (car car)

{

if (this. make = = car. Make && this. Model = = car. Model && this. Year = = car. Year)

{

return true;

}

Else

{

return false;

}

}

}

The definition of iequatable<t> does not provide an implementation for equals, which defines the signature only.

A class or struct can implement multiple interfaces, but a class can inherit only a single class (abstract or non-abstract).

An interface can contain methods, properties, events, indexers, or any combination of these four member types.

The interface member automatically becomes a public member and cannot contain any access modifiers. Members also cannot be static members.

To implement an interface member, the corresponding member of the implementation class must be public, non-static, and have the same name and signature as the interface member.

The interface has the following properties:

    • An interface is similar to an abstract base class. Any class or struct that implements an interface must implement all its members.
    • The interface cannot be instantiated directly. Its members are implemented by any class or struct that implements the interface.
    • An interface can contain events, indexers, methods, and properties.
    • An interface does not contain an implementation of a method.
    • A class or struct can implement multiple interfaces. A class can inherit one base class, and one or more interfaces can be implemented.
13.1 Class and interface inheritance

Any class derived from object actually inherits the following:

    • Method signature
    • Implementation of the method (ToString, Equals, GetHashCode, GetType)
13.2 Defining interfaces

The interface names a set of method signatures uniformly. Interfaces can also define events, no parameter properties, and indexers. All of this is essentially a method. However, an interface cannot define a constructor method. The interface also cannot define any instance fields.

C # prohibits an interface from defining any such static member.

In C #, the interface is defined with the interface keyword. To specify a name and a set of instance method signatures for the interface.

For the CLR, an interface definition is like a type definition. That is, the CLR defines an internal data structure for an interface type object, and it can use the reflection mechanism to query the functionality of the interface type.

As with types, interfaces can be defined within a file scope or nested within another type. When defining an interface type, you can specify any visibility/accessibility (public, protect, internal, etc.) that you want.

The interface member automatically becomes a public member and cannot contain any access modifiers. Members also cannot be static members.

By convention, the interface type name begins with an uppercase I to facilitate identification of the interface type in the source code.

The CLR supports generic interfaces and generic methods in interfaces.

13.3 Inheritance Interfaces

The following code shows how to define a type that implements the interface:

public interface Icomparable<in t>

{

An interface member cannot contain any access modifiers, it cannot be a static member, it automatically becomes a public member

int CompareTo (T other);

}

public sealed class Point:icomparable<point>

{

Private Int32 m_x, m_y;

An interface cannot define a constructor method, and a class that implements an interface can define a constructor method

Public point (Int32 x, Int32 y)

{

m_x = x;

m_y = y;

}

The member corresponding to the interface must be public and non-static, with the same name and signature as the interface member

Public Int32 CompareTo (point Other)

{

Return Math.sign (math.sqrt (m_x * m_x + m_y * m_y)

-Math.sqrt (other.m_x * other.m_x + other.m_y * other.m_y));

}

public override String ToString ()

{

Return String.Format ("{0},{1}", m_x, m_y);

}

}

public static Class program

{

public static void Main ()

{

Point[] points = new point[] {new Point (3, 3), New Point (1, 2)};

if (Points[0]. CompareTo (points[1]) > 0)

{

Point temppoint = Points[0];

Points[0] = points[1];

POINTS[1] = Temppoint;

}

Console.WriteLine ("Points from closest to (0,0) to farthest:");

foreach (point p in points)

Console.WriteLine (P);

}

}

The C # compiler requires that the method used to implement an interface be marked public.

The compiler marks the method that implements the interface as virtual and sealed.

13th Chapter Interface

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.