C#速成(之五)全文完

-------------------

委托

-------------------

委托让我们把一个函数引用存储在一个变量里。 C++ 当中,这类似于使用 typedef 定义的函数指针,我们通常用存储一个函数指针。

声明委托使用的关键字是 delegate 。瞧瞧这个示例,你会理解什么是委托:

示例:

delegate int Operation(int val1, int val2);

public int Add(int val1, int val2)

{

return val1 + val2;

}

public int Subtract (int val1, int val2)

{

return val1- val2;

}

public void Perform()

{

Operation Oper;

Console.WriteLine("Enter + or - ");

string optor = Console.ReadLine();

Console.WriteLine("Enter 2 operands");

string opnd1 = Console.ReadLine();

string opnd2 = Console.ReadLine();

int val1 = Convert.ToInt32 (opnd1);

int val2 = Convert.ToInt32 (opnd2);

if (optor == "+")

Oper = new Operation(Add);

else

Oper = new Operation(Subtract);

Console.WriteLine(" Result = {0}", Oper(val1, val2));

}

-------------------

继承和多态

-------------------

C# 仅允许单继承,多继承要通过接口来实现。

示例:

class Parent{

}

class Child : Parent

-------------------

虚拟方法

-------------------

除了在子类中实现虚拟方法采用 override 关键字外,虚拟方法实现多态的概念 C# 与 C++ 相同。父类使用相同的 virtual 关键字。从重载虚拟方法的每个类都要使用 override 关键字。

class Shape

{

public virtual void Draw()

{

Console.WriteLine("Shape.Draw") ;

}

}

class Rectangle : Shape

{

public override void Draw()

{

Console.WriteLine("Rectangle.Draw");

}

}

class Square : Rectangle

{

public override void Draw()

{

Console.WriteLine("Square.Draw");

}

}

class MainClass

{

static void Main (string[] args)

{

Shape[] shp = new Shape[3];

Rectangle rect = new Rectangle();

shp[0] = new Shape();

shp[1] = rect;

shp[2] = new Square();

shp[0].Draw();

shp[1].Draw();

shp[2].Draw();

}

}

输出 t:

Shape.Draw

Rectangle.Draw

Square.Draw

-------------------

** 使用 "new" 来隐藏父方法 **

-------------------

你可以定义一个子类成一个新方法版本,隐藏基类当中的那个版本。使用 new 关键字就可以定义一个新版本。思考下面的示例,它是上面示例的修改后的版本。注意当我用 Rectangle 类中的 new 关键字代替 override 关键字时示例的输出情况。

class Shape

{

public virtual void Draw()

{

Console.WriteLine("Shape.Draw") ;

}

}

class Rectangle : Shape

{

public new void Draw()

{

Console.WriteLine("Rectangle.Draw");

}

}

class Square : Rectangle

{

// 没在这里让你重载

public new void Draw()

{

Console.WriteLine("Square.Draw");

}

}

class MainClass

{

static void Main (string[] args)

{

Console.WriteLine("Using Polymorphism:");

Shape[] shp = new Shape[3];

Rectangle rect = new Rectangle();

shp[0] = new Shape();

shp[1] = rect;

shp[2] = new Square();

shp[0].Draw();

shp[1].Draw();

shp[2].Draw();

Console.WriteLine("Using without Polymorphism:");

rect.Draw();

Square sqr = new Square();

sqr.Draw();

}

}

输出 :

Using Polymorphism

Shape.Draw

Shape.Draw

Shape.Draw

Using without Polymorphism:

Rectangle.Draw

Square.Draw

这里的多态性不会把 Rectangle 类的 Draw 方法当做 Shape 的 Draw 方法多态性的一种表现。相反,它会认为这是一种不同的方法。因此,为了避免父类与子类间的命名冲突,我们使用了 new 修饰符。

注意:你不能使用同一类下面一种方法的两个版本,即一个是用 new 修饰符的版本,另一个是用 override 或 virtual 修饰符的版本。正象上面示例所说明的,我不能再在拥有 virtual 或 override 方法的 Rectangle 类中添加另一个命名为 Draw 的方法。同样地,在 Square 类中,我也不能重载 Square 类的虚拟的 Draw 方法。

-------------------

调用基类成员

-------------------

如果子类与基类有同名的数据成员,为避免命名冲突,访问基类数据成员和函要使用一个关键字 base 。在下面的示例中我们来看看如何调用基类的构造函数以及如何使用数据成员。

public Child(int val) :base(val)

{

myVar = 5;

base.myVar;

}

或者

public Child(int val)

{

base(val);

myVar = 5 ;

base.myVar;

}

--------------------------------

将来的补充:

本文仅是一个 C# 语言的快速概览,以便你能熟悉此语言的特点。虽然我已经尽力以简明而全面的代码例示方式讨论所有主要的 C# 概念,我认为要填加讨论的还有很多。

将来我会加入更多还没有讨论的命令和概念,比如事件等等。我也想写些有关 C# 初学者进行 Windows 编程的东东。

参考资料:

our most commonly known MSDN

Inside C# by Tom Archer

A Programmer's Introduction to C# by Eric Gunnerson

Beginning C# by Karli Watson

Programming C# (O'Reilly)

About the Author

Aisha is a Master of Science in Computer Science from Quaid-i-Azam Univeristy. She has worked in VC++ 6, MFC, ATL, COM/DCOM, ActiveX, C++, SQL, and so forth. These days she is working on .NET framework and C#. Inspired with nature, she loves to seek knowledge. She is also fond of travelling. She keeps a free source code and articles Web site at http://aishai.netfirms.com.

History

Date Posted: June 13, 2003

Published At
Categories with Web编程
Tagged with
comments powered by Disqus