作者在 2011-03-02 10:55:55 发布以下内容
1. C#中方法覆盖时override和new的比较
C#中override关键字使用在虚函数(virtual method)和抽象函数(abstract method)的覆盖上;
C#中new关键字使用在派生类覆盖父类的"普通"函数上(也就是说非虚函数非抽象函数的情境下)
下面是代码示例:(override的用法见下面abstract和virtual比较的章节)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
public abstract class Book
{
public void Introduce()
{
Console.WriteLine("I'm book");
}
public void SayHi()
{
Console.WriteLine("Hi, I'm book");
}
}
public class JavaBook : Book
{
// 显式加上new,跟不加new实际效果一样,只是加new之后会消除compile warning
public new void SayHi()
{
Console.WriteLine("Hi, I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
Book book = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
javaBook.SayHi(); //将调用JavaBook中SayHi()
book.Introduce(); //将调用Book中Introduce()
book.SayHi(); //将调用Book中SayHi()
}
public static void Main()
{
test t = new test();
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
public abstract class Book
{
public void Introduce()
{
Console.WriteLine("I'm book");
}
public void SayHi()
{
Console.WriteLine("Hi, I'm book");
}
}
public class JavaBook : Book
{
// 显式加上new,跟不加new实际效果一样,只是加new之后会消除compile warning
public new void SayHi()
{
Console.WriteLine("Hi, I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
Book book = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
javaBook.SayHi(); //将调用JavaBook中SayHi()
book.Introduce(); //将调用Book中Introduce()
book.SayHi(); //将调用Book中SayHi()
}
public static void Main()
{
test t = new test();
}
}
}
2. C#中abstract和virtual的比较
abstract方法(抽象方法)和Java中的使用是一致的,至少我现在是这么理解.
virtual是C#中为了实现运行时多态而加的机制,上面有提到,子类可以覆盖父类的函数(使用override)从而实现多态.
这里岔个话题,笔记一下C#中多态的实现:C#中多态的实现分为了两种,一种是编译时多态,一种是运行时多态
|- 编译时多态 通过一个类中多个方法的重载来实现的多态,系统在编译的时候,根据传递的参数确定具体调用哪个重载的方法;
|- 运行时多态 通过virtual函数(虚函数)、抽象方法实现的多态,派生类去override虚函数或抽象方法,从而实现运行时多态.
具体实例代码如下:
第一段代码: abstract的用法
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
public abstract class Book
{
//抽象方法,不含主体,抽象方法所在类必须为抽象类,派生类必须实现该方法
public abstract void Introduce();
}
public class JavaBook : Book
{
//实现抽象方法,必须实现,注意!必须添加override关键字
public override void Introduce()
{
Console.WriteLine("I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
Book book = new JavaBook();
book.Introduce(); //将调用JavaBook中Introduce()
}
public static void Main()
{
test t = new test();
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
public abstract class Book
{
//抽象方法,不含主体,抽象方法所在类必须为抽象类,派生类必须实现该方法
public abstract void Introduce();
}
public class JavaBook : Book
{
//实现抽象方法,必须实现,注意!必须添加override关键字
public override void Introduce()
{
Console.WriteLine("I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
Book book = new JavaBook();
book.Introduce(); //将调用JavaBook中Introduce()
}
public static void Main()
{
test t = new test();
}
}
}
第二段代码: virtual的用法以及override的用法
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
public abstract class Book
{
public virtual void Introduce()
{
Console.WriteLine("I'm book");
}
public virtual void SayHi()
{
Console.WriteLine("Hi, I'm book");
}
}
public class JavaBook : Book
{
public override void Introduce()
{
Console.WriteLine("I'm Java");
}
//注意,此方法没有override父类的方法
public void SayHi()
{
Console.WriteLine("Hi, I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
Book book = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
book.Introduce(); //将调用JavaBook中Introduce()
javaBook.SayHi(); //将调用JavaBook中SayHi()
book.SayHi(); //将调用Book中SayHi()
}
public static void Main()
{
test t = new test();
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
public abstract class Book
{
public virtual void Introduce()
{
Console.WriteLine("I'm book");
}
public virtual void SayHi()
{
Console.WriteLine("Hi, I'm book");
}
}
public class JavaBook : Book
{
public override void Introduce()
{
Console.WriteLine("I'm Java");
}
//注意,此方法没有override父类的方法
public void SayHi()
{
Console.WriteLine("Hi, I'm Java");
}
}
public class test
{
public test()
{
JavaBook javaBook = new JavaBook();
Book book = new JavaBook();
javaBook.Introduce(); //将调用JavaBook中Introduce()
book.Introduce(); //将调用JavaBook中Introduce()
javaBook.SayHi(); //将调用JavaBook中SayHi()
book.SayHi(); //将调用Book中SayHi()
}
public static void Main()
{
test t = new test();
}
}
}