C#速成(之五)全文完

    技术2022-05-11  16

    <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

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

    委托

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

    委托让我们把一个函数引用存储在一个变量里。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方法当做ShapeDraw方法多态性的一种表现。相反,它会认为这是一种不同的方法。因此,为了避免父类与子类间的命名冲突,我们使用了new修饰符。

     

    注意:你不能使用同一类下面一种方法的两个版本,即一个是用new修饰符的版本,另一个是用overridevirtual修饰符的版本。正象上面示例所说明的,我不能再在拥有virtualoverride方法的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

    <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

    最新回复(0)