Interface in C #

xiaoxiao2021-03-06  121

table of Contents

Interface definition

Interface and abstract class

Interface implementation

Polymorization of interface

One. Interface definition

Definition: Define an agreement. The class or structure that implements the interface must comply with its agreement.

It is simple to say a display definition that is complied with interactions between interfaces or classes. When the concept of "Class and Class Between Interface Interaction" is incorrect, it is incorrectly thought that the interface is a method of disclosure, and the class is interacting between the class. In fact, the interface is a definition independent of class. Interface definition class interaction between classes.

Then the class and the class is directly interacting, why should I use the interface?

This is mainly because the interface is an abstraction of interactive content between classes, and the content that needs interaction between classes is abstract to define the interface, which can better control the logical interaction between classes. It can be seen that the abstraction of interface content is related to the logic quality of the entire program; you can add new features at any time by developing additional interfaces and implementation;

About an important concept: Interface only contains members definition, does not include implementations of members, and members' implementation needs to be implemented in inheritance class or structure.

Members of the interface include: methods, features, indexers, events.

Note: The interface does not contain a field.

The class that implements the interface must strictly press each aspect of the interface.

Once the interface itself cannot be changed, it cannot be changed, and the published interface will be changed to destroy the existing code.

A typical interface example:

Using system;

Using system.collet;

Public Delegate Voic Chang (Object Sender, Object Event) / / Define a delegation

Public interface ibroker // Defines a stock economist interface

{

String getrating (String stock); // A method of getting a amount (not implemented here)

Decimal pricepertrade // Defines a feature set per share

{

Get; // Not implemented

SET;

}

Decimal this (string stockname) // definition indexer

{

Get;

SET;

}

Event change pricechange; // Define the event of the interface

}

two. Interface and abstract class

Abstracts and interfaces have many similar places in definition and function, and the use of abstraction classes in the program requires comparison of abstract classes and interfaces.

Abstract class: A class that cannot be instantiated and must be inherited from it, the abstract class can provide implementation, or not to provide

Subclasses can only inherit from an abstract class

Abstract class should be mainly used for close objects

If you want to design a large function unit, use an abstract class.

Create an abstract class if you are expected to create multiple versions of the component

Interface: It is a fully abstract member collection that does not provide awareness.

Class or structure can inherit several interfaces.

The interface is best suited for unrelated classes to provide universal features

Use interface if you want to design a small and concise function block

Once the interface is created, it cannot be changed. If you need new versions of the interface, you must create a new interface.

three. Interface implementation

The implementation of the interface is divided into: implicit implementation and explicit implementation. If the class or structure is to be implemented, you can use implicit implementation. If the class or structure inherits multiple interfaces, then the same name member is displayed in the interface. The display implementation implements the interface member by using the fully qualified name of the interface.

For the above example we can implement the interface:

Public class testinterface: ibroker // Define a class that inherited the iBroker interface

{

Hashtable hash = new hashtable ();

Decimal pricepertrade;

Public TestInterface (Decimal Price) // Constructor

{

Pricepertrade = price; // initialized string}

Public String getrating (String stock) // method for implicit implementation interface

{

Return "Buy";

}

Public decimal ibroker.pricepertrade // Explicitly realizes the characteristics of the interface

{

get

{

Return Pricepertrade;

}

set

{

Pricepertrade = Value;

PriceChange ("FinaceBroker", Value;

}

Public Decimal this (String stockname)

{

get

{

Return (Decimal) Hash [stockname];

}

set

{

Hash.add (stockname, value);

}

}

}

Public Event Changer PriceChange; // All members in the interface must be implemented

}

four. Polymorphism in the interface

Multiple classes inherited the same interface to implement the polymorphism of the interface, access to the interface of the interface and the polymorphic access of the class. The following example shows how to implement a polymorphic access:

Public Class Interfacetester

{

Public Strate (String [] ARGS)

{

String Recommendation;

ArrayList brokers = new arraylist; // Define a list

Brokers.Add (New FirstBroker); // Add the first inherited interface

Brokers.add (New SecondBroker); // Add a second inheritance interface

InterfaceTester new ution = new interface

Foreach (Ibroker Broker In Brokers)

{

Broker.PriceChange = New change (iftst.pricepertradechange);

Broker ["ADC"] = 12.33M;

Broker ["rs" = 11.23ml

Broker.pricepertrade = 12.55m;

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-126362.html

New Post(0)