C # Review ⑥
June 19, 2016
23:46
Main Interfaces & Delegates Interfaces and delegates
1. Interface Basic Syntax
Public Interfaceilist:icollection, IEnumerable {intADD (ObjectValue);//MethodsBOOLContains (Objectvalue), .....BOOLisreadonly {Get; }// Property...Object This[intIndex] {Get;Set; }//Indexer}
Interface equivalent to an abstract class, only the signature is not implemented;
Interface = purely abstract class; Only signatures, no implementation.
Interfaces may contain methods, properties, indexers, time (no fields, no constants, no constructors, no destructors, no operators, no cascading types)
May contain methods, properties, indexers and events
(No fields, constants, constructors, destructors, operators, nested types).
The interface member hides the abstract or virtual keyword
Interface members is implicitly public abstract (virtual).
Interface members cannot be static
Interface members must isn't be static.
Interfaces can inherit from other interfaces
Interfaces can inherit from the other Interfaces.
Classes and structs can implement multiple interfaces
Classes and structs may implement multiple interfaces.
2.Implemented by Classes and structs class, struct implementation interface
classMyclass:mybaseclass, IList, ISerializable { Public intADD (Objectvalue) {...} Public BOOLContains (Objectvalue) {...} ... Public BOOLisreadonly {Get {...} } ... Public Object This[intIndex] {Get{...}Set {...} }}
A class can inherit only one class but may implement multiple interfaces
A class can inherit from a single base class, but can implement multiple interfaces.
A struct cannot inherit from another class or struct but can implement multiple interfaces
A struct cannot inherit from any type, but can implement multiple interfaces.
Each member of an interface must be implemented
Every interface member (method, property, indexer) must is implemented or inherited from a base class.
Implementing the methods contained inside the interface is not necessarily declared as override
Implemented interface methods need not be declared as override.
Implementing the methods contained within an interface can be declared as abstract abstraction methods
Implemented interface methods can be declared as abstract (i.e. a interface can be implemented by an abstract class).
If the Add method in subclass MyClass should be overridden then it should be declared as virtual although the virtual keyword is already hidden in IList
If Add () should be overridden in a subclasses of MyClass it must being declared as virtual (although ADD () is already implici tly virtual in IList).
3.Working with Interfaces
Example:
InterfaceIsimplereader {intRead ();}InterfaceIreader:isimplereader {voidOpen (stringname);voidClose ();}classTerminal:isimplereader { Public intRead () {...}}classFile:ireader { Public intRead () {...} Public voidOpen (stringname) { ... } Public voidClose () {...}} Isimplereader SR=NULL;//NULL can assigned to any variable of a interface typeSR=NewTerminal (); SR=NewFile (); Ireader R=NewFile (); SR= R;
4.Delegate delegate
Declaration delegate: Delegate void Notifier (string sender);//composition: The signature of the common function plus the keyword delegate
Declaring a delegate variable: Notifier greeting;
To assign a method to a delegate variable:
void SayHello (string sender) {Console.WriteLine ("" +new Notifier (SayHello); // or just: // since C # 2.0
Call delegate variable: greeting ("John");
5. Different methods of allocation
Each method can be assigned to a delegate variable:
void Saygoodbye (string sender) {Console.WriteLine ("" += Saygoodbye;greetings ("John"); // Saygoodbye ("john") = "Good bye from John"
Note: The delegate variable can be assigned a null value;
If the delegate variable is null, then the delegate variable cannot be called, otherwise an exception is generated;
A delegate variable is actually a class that can store data in a structure that can pass parameters
Creating a Delegate Value:
m = obj. Method; or in long form:m = new Delegatetype (obj. Method);
A delegate variable can store a method and its receiver, not a parameter: Greetings = Myobj.sayhello;
If obj is this then it can be omitted: Greetings = SayHello;
The method can be static, but this requires a class name to be used as an instance, greetings = Myclass.staticsayhello;
The method cannot be abstract, but it can make the virtual,override,new
The signature of the method must match the signature of the delegate
Have the same number of parameters;
Have the same parameter type, including the return type;
Has the same parameter modifier (value,ref/out)
6. Multicast delegation Multicast Delegates
A delegate variable can master multiple methods at the same time;
=+ = saygoodbye;greetings ("John"); // "Hello from John" // "Good bye from John" -= sayhello;greetings ("John"); // "Good bye from John"
Attention:
If the multicast delegate is a function, then the return value is the last method to be called;
If the multicast delegate is an out-decorated parameter type, then the parameter should be the return of the last call. The parameter type of the ref modifier should be passed down from the method used.
The above features are implemented in Java:
7. Events
The event is a special delegate domain;
The event differs from the delegate variable:
Only the class declaring the event can dismiss the event;
Other classes can only change event fields using + = or-=
classModel { Public EventNotifier notifyviews; Public voidChange () {... notifyviews ("Model"); }}classView { PublicView (Model m) {m.notifyviews + =Update;}voidUpdate (stringSender) {Console.WriteLine (sender +"was changed"); }}classTest {Static voidMain () {Model Model=NewModel ();NewView (model);NewView (model), .... Model. Change ();}}
How events are handled in the. NET Library
Example:
Public Delegate voidKeyeventhandler (Objectsender, KeyEventArgs e); Public classKeyeventargs:eventargs { Public Virtual BOOLALT {Get{...} }//true if Alt key was pressed Public Virtual BOOLShift {Get{...} }//true if Shift key was pressed Public BOOLControl {Get{...} }//true if Ctrl key was pressed Public BOOLHandled {Get{...}Set{...} }//Indicates if event was already handled Public intKeyValue {Get{...} }//The typed key code...}classMykeyeventsource { Public EventKeyeventhandler KeyDown; KeyDown ( This,NewKeyEventArgs (...)); ...}classMykeylistener { PublicMykeylistener (...) {Keysource.keydown + =Handlekey;}voidHandlekey (Objectsender, KeyEventArgs e) {...}}
C # Review ⑥