继承可以实现一个父类被多个子类调用,减少重复代码!如:
class Program
{
static void Main(string[] args)
{
Person1 s1 = new Person1();
s1.name="lucy";
s1.Hoppy = "音乐";
s1.爱好();
Person2 s2 = new Person2();
s2.name = "Lily";
s2.Hoppy = "看电视";
s2.爱好();
Console.ReadKey();
}
}
class Person
{
public string name { set; get; }
public string Hoppy { set; get; }
public void 爱好()
{
Console.WriteLine("{0}的爱好是{1}",this.name,this.Hoppy);
}
}
class Person1 : Person
{
}
class Person2 : Person
{
} |