Interface in C #

xiaoxiao2021-03-06  47

The interface directory interface of the interface directory interface is a polymorphism that implements the interface of the abstract class interface. 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 the interface content is the logical quality of the entire program; you can add new features at any time by developing additional interfaces and implementations; about an important concept: Interface only includes members definition, not including members Realization, 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 the interface to implement the interface of each aspect of the interface. Once the published interface cannot be changed, the published interface will change the existing code. A typical interface example: use system; using system.colletion; public delegate Voic Chang (Object sender, object event) // Defines a commissioned public interface ibroker // Define a stock economist interface {String getrating (String stock); / / A method of obtaining the amount (not implemented here) Decimal PricePertRADE / / Define a characteristic of the price of each share {get; // does not implement the set;} Decimal this (String stockname) // Defines the indexer {Get; 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, and the abstract class can be implemented, or it is not possible to provide a subclass that can only inherit an abstract class from one abstraction class should be mainly used for close objects. If you want to design a big The function unit uses 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 to unrelated classes to provide generic feature If you want to design a small and concise function block, you cannot change once you created using the interface interface. If you need a new version of the interface, you must create a brand new interface. 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 an interface: public class testInterface: Ibroker // define a class ibroker interface inheritance {hashtable hash = new hashtable (); decimal pricepertrade; public testInterface (decimal price) // constructor {pricepertrade = price ; // Initialization string} public string getrating (string stock) // implicitly implemented interface method {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 All members of the Event Changer PriceChange; // must implement} 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 illustrates how to achieve polymorphism access interface: public class InterfaceTester {public stratic int Main (string [] args) {string recommendation; Arraylist Brokers = new Arraylist; // define a list Brokers.add (new firstBroker (7.21 m)))); // Add first inherited interface class brokers.add (New SecondBroker (12.3m)); // Add second inheritance interface InterfaceTester new iftst = new interface in brokers) { Broker.PriceChange = new change (iftst.pricepertradechange); Broker ["ADC"] = 12.33m; Broker ["RTY"] = 11.23ml Broker.pricepertrade = 12.55m;}}}}}

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

New Post(0)