The use and understanding of interfaces in C #

Source: Internet
Author: User

An interface is a definition of a contract, just a shelf, which must be implemented by a struct or class.

a definition and use of interfaces

Example: Define an interface

Interface Border

{

int weight//not implemented

{

Set

Get

}

int height//not implemented

{

Set

Get

}

}//do nothing, just tell you two properties weight,height, to use this interface, it must be implemented.

Class Square:border//attention to the implementation of the interface

{

private int x;

private int y;

public int Weight

{

Get

{

return x;

}

Set

{

x = value;

}

}

public int Height

{

Get

{

return y;

}

Set

{

y = value;

}

}

Application of the interface

Border B = new Square ();

B.weight = 2;

B.height = 2;

Console.WriteLine ("Heighe is {0,weight is {1}", b.height,b.weight);

The above simple interface is an example. After running, believe that there should be a simple concept for the interface.

two Considerations for Interfaces

1 The implementation class must implement all members of the interface, such as the weight and height properties in the previous example.

2 interfaces cannot include variable members , only properties, events, methods, indexers (only two properties for the example above)

3 The class implementing the interface must be implemented in strict accordance with the definition of the interface

4 implementing an interface can be implemented implicitly and explicitly

For example:

Interface IControl//interface 1

{

void Paint ();

}

Interface ISurface//interface 2

{

void Paint ();

}

Note: Both of these interfaces contain the method paint (), which must be explicitly implemented in the following implementation class

Class Sampleclass:icontrol, ISurface

{

Void Paint ()//This implicit implementation is certainly prone to ambiguity

void IControl. Paint ()//EXPLICIT implementation

{

System.Console.WriteLine ("IControl.Paint");

}

void ISurface. Paint ()//EXPLICIT implementation

{

System.Console.WriteLine ("Isurface.paint");

}

}

After a member in the 5 interface is explicitly implemented, it must be called through an interface and not directly through the class

In the above example SampleClass SP = new SampleClass ();

Sp.paint () This is wrong, you must IControl I1 = (IControl) SP, and then I1. Paint ();

61 interfaces can be implemented by several classes, or only one class can be implemented, or it can be called an interface polymorphism

71 implementation classes can implement several interfaces at the same time, or they can implement a unique interface

8 There is no arbitrary change after the interface definition of the implementation class, otherwise it is easy to disrupt the implementation class.

three examples of completing interface exercises:

Using System;

Using System.Collections.Generic;

Using System.Text;

Interface Border//define Interface

{

int weight

{

Set

Get

}

int height

{

Set

Get

}

}

interface area//definition interface

{

int Reareavalue ();

}

Interface Perimeter//define Interface

{

int Reperimetervalue ();

}

interface px//define Interface

{

String Pring ();

}

Interface IControl//define Interface

{

void Paint ();

}

Interface ISurface//define Interface

{

void Paint ();

}

Class Square:border,area,perimeter//implementation interface, simultaneous implementation of Border,area,perimeter three interfaces

{

private int x;

private int y;

public int Weight

{

Get

{

return x;

}

Set

{

x = value;

}

}

public int Height

{

Get

{

return y;

}

Set

{

y = value;

}

}

public int Reareavalue ()

{

return x * y;

}

public int Reperimetervalue ()

{

return (x + y) * 2;

}

}

Class Tempclass:area//Implement Interface, implement area interface again [polymorphism]

{

public int x = 3;

public int Reareavalue ()

{

return x*x;

}

}

Class Sampleclass:icontrol, ISurface//implementation interface IControl, ISurface

{

void IControl.Paint ()//Display implementation

{

System.Console.WriteLine ("IControl.Paint");

}

void Isurface.paint ()//Display implementation

{

System.Console.WriteLine ("Isurface.paint");

}

}

Class MainClass

{

static void Wrtieborder (Border b)

{

Console.WriteLine ("Heighe is {0,weight is {1}", b.height,b.weight);

}

static void Writearea (Area a)

{

Console.WriteLine ("A.reareavalue () is {0}", A.reareavalue ());

}

static void Writeperimeter (Perimeter p)

{

Console.WriteLine ("P.reperimetervalue () is {0}", P.reperimetervalue ());

}

static void Main ()

{

Square S1 = new Square ();

S1.weight = 2;

S1.height = 2;

Wrtieborder (S1);

Writeperimeter (S1);

Writearea (S1);

Tempclass temp = new Tempclass ();

Console.WriteLine ("Main Tempclass object temp. Reareavalue () is "+ temp. Reareavalue ());

Writearea (temp);

Border B = new Square ();

B.weight = 5;

B.height = 6;

Wrtieborder (b);

SampleClass obj = new SampleClass ();

IControl C = (IControl) obj;

C.paint ();

ISurface s = (isurface) obj;

S.paint (); Calls Isurface.paint on SampleClass.

Console.ReadLine ();

}

}

Four interfaces and abstract classes

There are many similarities between interfaces and abstract classes, and there are different places: the following

Abstract class: A class that cannot be instantiated and must inherit from, an abstract class can provide an implementation, or it may not provide an implementation

subclasses can inherit from only one abstract class

Abstract classes should be used primarily for closely related objects

If you are designing large functional units, use an abstract class.

If you expect to create multiple versions of a component, create an abstract class

Interface: is a fully abstracted collection of members and does not provide realization of realizations.

A class or struct can inherit several interfaces.

Interfaces are best suited to provide common functionality for unrelated classes

If you want to design a small and concise function block, use the interface

Five Summary

interface, the following is where we have to remember the heart, the interface contains only the signature of the method, delegate, or event. The implementation of a method is accomplished in the class that implements the interface, which can be a member of a namespace or class, and can contain signatures of the following members: Methods, properties, indexers, events, and an interface can inherit from one or more base interfaces. When a base type list contains a base class and an interface, the base class must be the first item in the list.

A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an interface instance.

The use and understanding of interfaces in C #

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.