C#-继承
作者:互联网
实现
class Teat1
{
//父类
}
class Test2 : Test
{
//子类
}
base关键字
base.property;//调用父类属性
base.method();//调用父类方法
class Computer //父类:电脑
{
public string sayHello()
{
return "欢迎使用";
}
}
class Pad : Computer //子类:平板电脑
{
public new string sayHello() //子类重写父类方法
{
return base.sayHello() + "平板电脑"; //调用父类方法,在结果后添加字符串
}
}
class Program
{
static void Main(string[] args)
{
Computer pc = new Computer(); //电脑类
Console.WriteLine(pc.sayHello());
Pad ipad = new Pad(); //平板电脑类
Console.WriteLine(ipad.sayHello());
Console.ReadLine();
}
}
标签:Computer,C#,sayHello,继承,base,子类,父类,class 来源: https://www.cnblogs.com/shazamsjtu/p/16644201.html