惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 豆浆

C#速成(之四) C#速成(之三) C#速成(之一) RegExp ASP后遗症种种 Visual SourceSafe如何支持并行开发 在 ASP.NET 中执行 URL 重写 How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET New Security Features in ASP.NET 2.0 Storing User Information with ASP.NET 2.0 Profiles Take Advantage of ASP.NET Built-in Features to Fend Off Web Attacks (MS SQL Server)SQL语句导入导出大全 一个美国老工程师的心理话: 给年轻工程师的十大忠告 很强的c#.net函数列表 微软开发者系列讲座下载 国产软件陷入3大争论 反垄断法出台迫在眉睫 国内软件业陷入“三输”怪圈 正走向慢性自杀 联想欲借自主知识产权操作系统“麒麟”挺直腰杆 今年中国“十大并购
C#速成(之五)全文完
豆浆 · 2005-03-27 · via 博客园 - 豆浆

委托

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

委托让我们把一个函数引用存储在一个变量里。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

版权声明:CSDN是本Blog托管服务提供商。如本文牵涉版权问题,CSDN不承担相关责任,请版权拥有者直接与文章作者联系解决。