电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> C#>>C#基础全接触之二:

C#基础全接触之二

来源:www.cncfan.com | 2006-8-30 | (有1872人读过)

例:假设一个Invoice发票类由多个InvoiceDetailLine类(成员只有一个Double类型的Amount金额属性)组成,
我们重载+操作符,使之可以将InvoiceDetailLine类的内容(注意不是金额合计)加在一起。
class Invoice
{
public ArrayList DetailLine;

public Invoice //类的析构函数
{
DetailLine = new ArrayList(); //ArrayList存放多个InvoiceDetailLine类的实例
}

public static Invoice operator+ (Invoice Invoice1, Invoice Invoice2) //参数与返回值的类型一致
{
//Invoice1与Invoice2的内容合并
Invoice ReturnInvoice = new Invoice();
foreach(InvoiceDetailLine detailLine in Invoice1.DetailLines)
ReturnInvoice.DetailLine.Add(detailLine);
foreach(InvoiceDetailLine detailLine in Invoice2.DetailLines)
ReturnInvoice.DetailLine.Add(detailLine);
return ReturnInvoice;
}
}

class InvoiceAddApp //调用示例
{
public static void main()
{
Invoice i1 = new Invoice();
for(int i = 0; i < 3; i++)
i1.DetailLine.Add(new InvoiceDetailLine(i + 1));

Invoice i2 = new Invoice();
for(int i = 0; i < 3; i++)
i2.DetailLine.Add(new InvoiceDetailLine(i + 1));

Invoice summaryInvoice = i1 + i2; //调用重载的操作符+方法
}
}


自定义类型转换可以编写代码实际二个不同的类、结构体之间的转换。
语法:public static implicite/explicite operator 输出类型 (输入类型)
说明:
1)转换方法必须是静态的。
2)implicite表示隐式转换,explicite表示显式转换。
3)输入类型和输出类型其中之一必须与包含转换的类或结构体类型。即转换必须与本类相关。
例:
struct Celisus
{
public float t;

public Celisus(float t)
{
this.t = t; //this.t是结构体的字段,t是参数
}

public static implicite operator Celisus(float t) //float=>Celisus
{
return new Celisus(t);
}

public static implicite operator float(Celisus c) //Celisus=>float
{
return ((c.t - 32) / 9) * 5;
}
}


代表的(delegate)目的与C++中的函数指针相同,代表不是在编译时被定义的,而是在运行时被定义的。
代表主要有二个用途:回调(Callback)和事件处理(event)
回调通常用于异步处理和自定义处理。例:
class DBManager
{
static DBConnection[] activeConnections;
//声明回调函数
public void delegate EnumConnectionCallback(DBConnection connection);

public static void EnumConnections(EnumConnectionCallback callback)
{
foreach (DBConnection connection in activeConnections)
{
callback(connection); //执行回调函数
}
}
}

//调用
class DelegateApp
{
public static void ActiveConncetionCallback(DBConnection connection) //处理函数
{
...
}

public void main()
{
//创建指向具体处理函数的代表实例(新建一个代表,让它指向具体的处理函数)
DBManager.EmnuConnectionCallback myCallback = new DBManager.EmnuConnectionCallback(ActiveConncetionCallback);
DBManager.EnumConnections(myCallback);
}
}

//使用静态代表,上面的调用改为
class DelegateApp
{
//创建一个指向处理函数的静态代表
public static DBManager.EmnuConnectionCallback myCallback
= new DBManager.EmnuConnectionCallback(ActiveConncetionCallback);
public static void ActiveConncetionCallback(DBConnection connection)
{
...
}

public void main()
{
DBManager.EnumConnections(myCallback);
}
}

//在需要时才创建代表,上面的调用改为
class DelegateApp
{
//将创建代表放在属性的getter方法中
public static DBManager.EmnuConnectionCallback myCallback
{
get
{
retun new DBManager.EmnuConnectionCallback(ActiveConncetionCallback);
}
}
public static void ActiveConncetionCallback(DBConnection connection)
{
...
}

public void main()
{
DelegateApp app = new DelegateApp(); //创建应用程序
DBManager.EnumConnections(myCallback);
}
}


可以将多个代表整合成单个代表,例:
class CompositeDelegateApp
{
public static void LogEvent(Part part)
{
...
}

public static void EmailPurchasingMgr(Part part)
{
...
}

public static void Main()
{
//定义二个代表
InventoryManager.OutOfStockExceptionMethod LogEventCallback
= new InventoryManager.OutOfStockExceptionMethod(LogEvent);
InventoryManager.OutOfStockExceptionMethod EmailPurchasingMgrCallback
= new InventoryManager.OutOfStockExceptionMethod(EmailPurchasingMgr);
//整合为一个代表,注意后加的代表先执行(这里是先执行LogEventCallback)
InventoryManager.OutOfStockExceptionMethod onHandExceptionEventsCallback
= EmailPurchasingMgrCallback + LogEventCallback;
//调用代表
InventoryManager mgr = new InventoryManager();
mgr.ProcessInventory(onHandExceptionEventsCallback);
//InventoryManager类的ProcessInventory方法的原型为:
//public void ProcessInventory(OutOfStockExceptionMethod exception);
}
}

可以根据需要将多个代表自由地组合成单个代表,例:
class CompositeDelegateApp
{
//代表指向的处理函数(三个代表三个函数)
public static void LogEvent(Part part)
{
...
}

public static void EmailPurchasingMgr(Part part)
{
...
}

public static void EmailStoreMgr(Part part)
{
...
}

public static void Main()
{
//通过数组定义三个代表
InventoryManager.OutOfStockExceptionMethod[] exceptionMethods
= new InventoryManager.OutOfStockExceptionMethod[3];
exceptionMethods[0] = new InventoryManager.OutOfStockExceptionMethod(LogEvent);
exceptionMethods[1] = new InventoryManager.OutOfStockExceptionMethod(EmailPurchasingMgr);
exceptionMethods[2] = new InventoryManager.OutOfStockExceptionMethod(EmailStoreMgr);

int location = 1;
//再定义一个代表(用于组合成单代表)
InventoryManager.OutOfStockExceptionMethod compositeDelegate;
//根据需要组合
if (location = 2)
{
compositeDelegate = exceptionMethods[0] + exceptionMethods[1];
}
else
{
compositeDelegate = exceptionMethods[0] + exceptionMethods[2];
}
//调用代表
InventoryManager mgr = new InventoryManager();
mgr.ProcessInventory(compositeDelegate);
}
}
C#的事件遵循“发布——预订”的设计模式。在这种模式中,一个类公布能够出现的所有事件,
然后任何的类都可以预订这些事件。一旦事件产生,运行环境就负责通知每个订户事件已经发生了。
当代表作为事件的处理结果时(或者说定义具有代表的事件),定义的代表必须指向二个参数的方法:
一个参数是引发事件的对象(发布者),另一个是事件信息对象(这个对象必须从EventArgs类中派生)。
例:
using System;

class InventoryChangeEventArgs: EventArgs //事件信息对象,从EventArgs类派生
{
... //假设定义二个public属性string Sku和int Change
}

C#热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号