1.子类方法对父类方法的 隐藏 与 重写
1.当子类的方法要 隐藏 父类的方法时可以在子类的方法前面添加 new 关键字2.当子类的方法要 重写 父类的方法时可以在子类的方法前面添加 override 关键字,同时在父类的方法面前添加 virtual 关键字(重写父类的虚方法)..................子类在继承父类的方法时会重写父类的方法
2.抽象类 与 抽象方法
1.抽象方法不用写方法体...需要在类的前面也加上 abstract 关键字,使当前类变成 抽象类2.子类在继承包含抽象方法的抽象类时,子类必须要 重写父类所有的抽象方法(即实现抽象方法体)
3.抽象类不能实例化.........即不能使用 new 关键字
3.接口
1.接口直接添加 Interface 关键字...接口的命名一般在名称的前面添加 I .2.使用显式接口成员执行体通常有两个目的:
1.因为显式接口成员执行体不能通过类的实例进行访问,这就可以从公有接口中把接口的实现部分单独分离开。如果一个类只在内部使用该接口,而类的使用者不会直接使用到该接口,这种显式接口成员执行体就可以起到作用。
2.显式接口成员执行体避免了接口成员之间因为同名而发生混淆。如果一个类希望对名称和返回类型相同的接口成员采用不同的实现方式,这就必须要使用到显式接口成员执行体。如果没有显式接口成员执行体,那么对于名称和返回类型不同的接口成员,类也无法进行实现。
注意:类只能继承一个类....但可以实现多个接口类
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestChild tc = new TestChild();
TestParent tp = new TestChild(); //这个对象只能调用TestParent 中包含的方法,不能调用TestChild 中有的而 TestParent 中没有的方法
string strReturn = tc.ShowVirtual("new TestChild().ShowVirtual") + "<br />" + tp.ShowVirtual("new TestChild().ShowVirtual") + "<br />";
strReturn += tp.ShowAbstract("ShowAbstract") + "<br />" + tc.ShowAbstract("ShowAbstract") + "<br />";
strReturn += tc.ShowInterface("new TestChild") + "<br />" + ((ITestInterface)tp).ShowInterface("((ITestInterface)tc).ShowInterface()") + "<br />";
strReturn += "索引器的使用:"+tc[0];
Response.Write("<p>面向对象(Object Oriented,OO)</p>" + strReturn);
}
}
public abstract class TestParent
{
//虚方法
public virtual string ShowVirtual(string txt) { return "TestParent...." + txt; }
//抽象方法
public abstract string ShowAbstract(string txt);
}
public class TestChild : TestParent, ITestInterface
{
private string[] name = new string[6] { "dong", "zhu", "qing", "chen", "xiong", "zhong" };
//这里是索引器的使用方式....
public string this[int index]
{
get
{
return index >= 0 && index <= 5 ? name[index] : null;
}
//set { /* set the specified index to value here */ }
}
public override string ShowVirtual(string txt) { return base.ShowVirtual("TestChild...." + txt); }
public override string ShowAbstract(string txt)
{
return "TestChild.........." + txt;
}
//实现接口
public string ShowInterface(string txt)
{
return "TestChild.ShowInterface().........." + txt;
}
//显式实现接口
string ITestInterface.ShowInterface(string txt)
{
return "ITestInterface.ShowInterface(string txt).........." + txt;
}
}
interface ITestInterface
{
string ShowInterface(string txt);
}
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestChild tc = new TestChild();
TestParent tp = new TestChild(); //这个对象只能调用TestParent 中包含的方法,不能调用TestChild 中有的而 TestParent 中没有的方法
string strReturn = tc.ShowVirtual("new TestChild().ShowVirtual") + "<br />" + tp.ShowVirtual("new TestChild().ShowVirtual") + "<br />";
strReturn += tp.ShowAbstract("ShowAbstract") + "<br />" + tc.ShowAbstract("ShowAbstract") + "<br />";
strReturn += tc.ShowInterface("new TestChild") + "<br />" + ((ITestInterface)tp).ShowInterface("((ITestInterface)tc).ShowInterface()") + "<br />";
strReturn += "索引器的使用:"+tc[0];
Response.Write("<p>面向对象(Object Oriented,OO)</p>" + strReturn);
}
}
public abstract class TestParent
{
//虚方法
public virtual string ShowVirtual(string txt) { return "TestParent...." + txt; }
//抽象方法
public abstract string ShowAbstract(string txt);
}
public class TestChild : TestParent, ITestInterface
{
private string[] name = new string[6] { "dong", "zhu", "qing", "chen", "xiong", "zhong" };
//这里是索引器的使用方式....
public string this[int index]
{
get
{
return index >= 0 && index <= 5 ? name[index] : null;
}
//set { /* set the specified index to value here */ }
}
public override string ShowVirtual(string txt) { return base.ShowVirtual("TestChild...." + txt); }
public override string ShowAbstract(string txt)
{
return "TestChild.........." + txt;
}
//实现接口
public string ShowInterface(string txt)
{
return "TestChild.ShowInterface().........." + txt;
}
//显式实现接口
string ITestInterface.ShowInterface(string txt)
{
return "ITestInterface.ShowInterface(string txt).........." + txt;
}
}
interface ITestInterface
{
string ShowInterface(string txt);
}