继承父类使用关键字virtual和new的区别。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
testClass c1 = new testClass1();
Console.Write(c1.name);
Response.Write(c1.name+"<br/>");
a a1 = new b();
Response.Write(a1.sayHello());
}
}
public class testClass
{
public virtual string name
{
get { return "111"; }
}
public virtual void Method()
{
string s = this.name; //s是多少
}
}
public class testClass1 : testClass
{
public override string name
{
get
{
return "222";
}
}
public void aaa()
{
this.Method();
}
public override void Method()
{
base.Method();
}
}
public class a
{
public string sayHello()
{
return "a say hello! ";
}
}
public class b : a
{
new public string sayHello()
{
return "b say hello! ";
}
}
结果是:
222
a say hello!