c# 继承
class person //默认不写继承基类object,只能继承一个类。类的单根性。
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public int Hight
{
get;
set;
}
public void Eat()
{
Console.WriteLine("eat");
}
public void Sleep()
{
Console.WriteLine("sleep");
}
}
class student:person
{
public void sayhi()
{
}
}
class teacher:person
{
public double Gonghao
{
get;
set;
}
}
class person2 //子类默认情况下,在自己调用前都会会调用无参数的构造函数,如没有则提示报错 解决办法一在父类参加一个无参的构造函数 解决办法二 base 调用父类的构造函数 不能被继承
{
public person2()//无参数构造函数
{
}
public person2(string name,int age,int height)
{
this.Name=name;
this.Age=age;
this.Hight=height;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public int Hight
{
get;
set;
}
public void Eat()
{
Console.WriteLine("eat");
}
public void Sleep()
{
Console.WriteLine("sleep");
}
}
class student2 : person2
{
public student2(string name,int age,int height,string sid ):base(name,age,height)
{
this.sid = sid;
//this.Name = name;
//this.Hight = height;
//this.Age = age;
}
public string sid
{
get;
set;
}
}
class teacher2 :person2
{
public teacher2(string name,int age,int height,string empid)
{
this.Name = name;
this.Age = age;
this.Hight = height;
this.empid = empid;
}
public string empid
{
get;
set;
}
}
//使用this调用自己的构造函数
class person3
{
public person3(string name,int age):this(name,1,"00",0)
{
this.Name=name;
this.Age=age;
}
public person3(string name,int age,string email, int height)
{
this.Age = age;
this.Email = email;
this.Name = name;
this.Hight = height;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public int Hight
{
get;
set;
}
public string Email
{
get;
set;
}
}