namespace 继承2
{
class Program
{
static void Main(string[] args)
{
MyInterest iter = new MyInterest();
iter.Interest();
Console.ReadLine();
}
public class MyInterest //定义我的爱好的类
{
public virtual void Interest()
{
Console.WriteLine(" This is my hobby");
Console.ReadLine();
}
}
public class toProgram : MyInterest //定义我的爱好的类的子类 编程
{
public override void Interest()
{
Console.WriteLine("I like programme");
}
}
public class Write : MyInterest //定义我的爱好的类的子类 写字
{
public override void Interest()
{
Console.WriteLine("i like write chinese characters");
}
}
public class Eate : MyInterest //定义我的爱好的类的子类 吃的
{
public override void Interest()
{
Console.WriteLine("Rice IS my best love");
}
}
public class Sport : MyInterest //定义我的爱好的类的子类运动
{
public override void Interest()
{
Console .WriteLine ("i like pingpang");
}
}
}
}
override的是重写的意思,你的三个子类都重写了父类的Interest()方法,目测会执行最后一个重写的。
public ovverride void Interest()
{
Console.WriteLine("i like pingpang");
}
额,我的本意是要把他么全部写出来,这个要怎么改,而且出来的结果是父类中的This is my hobby
public override void Interest()
{
Console.WriteLine ("i like pingpang");
base.Interest();
}
别的类似
在C#中定义了子类去调用父类的方法时,会先去执行父类的方法之后再去执行子类的方法