程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#復習⑥,

C#復習⑥,

編輯:C#入門知識

C#復習⑥,


C#復習⑥

2016年6月19日

23:46

Main Interfaces & Delegates 接口和委托

1.接口基本語法

public interface IList : ICollection, IEnumerable {

int Add (object value);        // methods

bool Contains (object value);

...

bool IsReadOnly { get; }        // property

...

object        this [int index] { get; set; }        // indexer

}

接口相當於一個抽象類,只有簽名沒有實現;

Interface = purely abstract class; only signatures, no implementation.

接口可能包含方法、屬性、索引器、時間(沒有字段、沒有常量、沒有構造函數、沒有析構函數、沒有運算符、沒有級聯類型)

May contain methods, properties, indexers and events
(no fields, constants, constructors, destructors, operators, nested types).

接口成員隱藏著abstract或者virtual關鍵字

Interface members are implicitly public abstract (virtual).

接口成員不能為static

Interface members must not be static.

接口可以繼承自其他的接口

Interfaces can inherit from other interfaces.

類和結構體可以實現多個接口

Classes and structs may implement multiple interfaces.

2.Implemented by Classes and Structs類、結構體實現接口

class MyClass : MyBaseClass, IList, ISerializable {

public int Add (object value) {...}

public bool Contains (object value) {...}

...

public bool IsReadOnly { get {...} }

...

public object this [int index] { get {...} set {...} }

}

 

一個類只能繼承一個類但是可以實現多個接口

A class can inherit from a single base class, but can implement multiple interfaces.

一個結構體不能繼承自其他的類或者結構體但是可以實現多個接口

A struct cannot inherit from any type, but can implement multiple interfaces.

接口中的每一個成員必須被實現

Every interface member (method, property, indexer) must be implemented or inherited from a base class.

實現接口內部包含的方法不必要聲明為override

Implemented interface methods need not be declared as override.

實現接口內部包含的方法可以聲明為abstract抽象方法

Implemented interface methods can be declared as abstract (i.e. an interface can be implemented by an abstract class).

如果子類MyClass中Add方法應該被重寫那麼應當聲明為virtual盡管在IList中已經隱藏著virtual關鍵字

If Add() should be overridden in a subclasses of MyClass it must be declared as virtual (although Add() is already implicitly virtual in IList).

3.Working with Interfaces

舉例:

interface ISimpleReader {

int Read();

}

interface IReader : ISimpleReader {

void Open(string name);

void Close();

}

class Terminal : ISimpleReader {

public int Read() { ... }

}

class File : IReader {

public int Read() { ... }

public void Open(string name) { ... }

public void Close() { ... }

}

ISimpleReader sr = null;        // null can be assigned to any variable of an interface type

sr = new Terminal();

sr = new File();

IReader r = new File();

sr = r;

4.Delegate委托

聲明委托:delegate void Notifier (string sender);//組成:普通函數的簽名加上關鍵字delegate

聲明委托變量:Notifier greeting;

將方法分配給委托變量:

void SayHello(string sender) {

Console.WriteLine("Hello from " + sender);

}

greetings = new Notifier(SayHello);        // or just:

greetings = SayHello; // since C# 2.0

調用委托變量:greeting("John");

5.不同的方法分配

每一個方法可以分配至一個委托變量:

void SayGoodBye(string sender) {

Console.WriteLine("Good bye from " + sender);

}

greetings = SayGoodBye;

greetings("John");        // SayGoodBye("John") => "Good bye from John"

注意:委托變量可以被賦予null值;

如果委托變量為null,那麼該委托變量不能被調用,否則產生異常;

委托變量其實是一種類,可以存儲的數據結構中,可以傳遞參數

Creating a Delegate Value:

m = obj.Method; // or in long form: m = new DelegateType (obj.Method);

一個委托變量可以存儲一個方法以及它的接收器,不是參數:greetings = myObj.SayHello;

如果obj是this那麼可以省略:greetings = SayHello;

方法可以是靜態的,但是這樣需要用類名來進行實例,greetings = MyClass.StaticSayHello;

方法不能是抽象的,但是可以使virtual,override,new

方法的簽名必須和委托的簽名相匹配

有相同的參數個數;

有相同的參數類型,包括返回類型;

有相同的參數修飾符(value,ref/out)

6.多播委托Multicast Delegates

一個委托變量可以同時掌握著多個方法;

Notifier greetings;

greetings = SayHello;

greetings += SayGoodBye;

greetings("John");        // "Hello from John"

// "Good bye from John"

greetings -= SayHello;

greetings("John");        // "Good bye from John"

注意:

如果多播委托是一個函數,那麼返回值是最後一個被調用的那個方法;

如果多播委托是out修飾的參數類型,那麼參數應該是最後一個調用的返回.ref修飾的參數類型應該從所用的方法一直傳遞下去。

Java中實現上述功能:

7.事件

事件就是特殊的委托域;

事件與委托變量不同的地方:

只有聲明事件的類才能夠解除事件;

其他類只能改變事件域只能使用+= 或者 -=

class Model {

public event Notifier notifyViews;

public void Change() { ... notifyViews("Model"); }

}

class View {

public View(Model m) { m.notifyViews += Update; }

void Update(string sender) { Console.WriteLine(sender + " was changed"); }

}

class Test {

static void Main() {

Model model = new Model();

new View(model); new View(model); ...

model.Change();

}

}

事件在.NET Library中如何處理

public delegate void KeyEventHandler (object sender, KeyEventArgs e); public class KeyEventArgs : EventArgs { public virtual bool Alt { get {...} } // true if Alt key was pressed public virtual bool Shift { get {...} } // true if Shift key was pressed public bool Control { get {...} } // true if Ctrl key was pressed public bool Handled { get{...} set {...} } // indicates if event was already handled public int KeyValue { get {...} } // the typed key code ... } class MyKeyEventSource { public event KeyEventHandler KeyDown; ... KeyDown(this, new KeyEventArgs(...)); ... } class MyKeyListener { public MyKeyListener(...) { keySource.KeyDown += HandleKey;} void HandleKey (object sender, KeyEventArgs e) {...} }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved