第二十节 继承
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
案例1:继承 namespace e1 { public enum Genders { Male,Female } class Person { private string name; public string Name { set { name = value; } get { return name; } } private int age; public int Age { set { age = value; } get { return age; } } private Genders gender; public Genders Gender { set { gender = value; } get { return gender; } } } } —————————————————————————————————————————————————————— namespace e1 { //C#中继承使用“:”,一旦继承自一个类后意味着当前就拥有了父类的公开成员 class Student:Person { //因为继承了父类一些共性的特征和行为,在子类中我们只需编写子类特有的状态和行为即可! private String hobby; public string Hobby { set { hobby = value; } get { return hobby; } } private int popularity; public int Popularity { set { popularity = value; } get { return popularity; } } public void SayHi() { Console.WriteLine("姓名:{0},年龄:{1},性别:{2},爱好:{3},受欢迎度:{4}",Name,Age,Gender,Hobby,Popularity); } } } —————————————————————————————————————————— namespace e1 { class Teacher:Person { private decimal salary; public decimal Salary { set { salary = value; } get { return salary; } } private int serviceOfYear; public int ServiceOfYear { set { serviceOfYear = value; } get { return serviceOfYear; } } public void SayHi() { Console.WriteLine("姓名:{0},年龄:{1},性别:{2},薪水:{3},工作年限:{4}",Name,Age,Gender,Salary,ServiceOfYear); } } } —————————————————————————————————————————————————————— namespace e1 { class Adviser:Person { private string duty; public string Duty { set { duty = value; } get { return duty; } } public void SayHi() { Console.WriteLine("姓名:{0},年龄:{1},性别:{2},职责:{3}",Name,Age,Gender, Duty); } } } ———————————————————————————————————————————————————————— namespace e1 { /// <summary> /// 继承 /// </summary> class Program { static void Main(string[] args) { Student stu = new Student(); stu.Name = "张见见"; stu.Age = 20; stu.Gender = Genders.Male; stu.Hobby = "唱个歌"; stu.Popularity = 101; stu.SayHi(); Teacher tea = new Teacher(); tea.Name = "周老师"; tea.Age = 25; tea.Gender = Genders.Female; tea.Salary = 5000; tea.ServiceOfYear = 10; tea.SayHi(); Adviser ads = new Adviser(); ads.Name = "龙龙"; ads.Age = 35; ads.Gender = Genders.Female; ads.Duty = "心里指导师"; ads.SayHi(); Console.ReadKey(); } } } ———————————————————————————————————————————————————————————————————————————————————————— 案例2: namespace e3 { class Vertebrata { private double bodyWeight; public double BodyWeight { set { bodyWeight = value; } get { return bodyWeight; } } private double bodyTemparature; public double BodyTemparature { set { bodyTemparature = value; } get { return bodyTemparature; } } public Vertebrata() { this.bodyTemparature = 50.3; this.bodyWeight = 300; } public void Breathe() { Console.WriteLine("呼吸"); } public void Eat() { Console.WriteLine("进食"); } public void Sleep() { Console.WriteLine("睡觉"); } } } —————————————————————————————————————————— namespace e3 { class Manmal:Vertebrata { private string skin; public string Skin { set { skin = value; } get { return skin; } } public Manmal() { this.skin = "毛皮"; } public void Run() { Console.WriteLine("奔跑"); } public void Sukle() { Console.WriteLine("哺乳"); } } } ———————————————————————————————————————————— namespace e3 { class Human :Manmal { private string limbs; public string Limbs { set { limbs = value; } get { return limbs; } } public Human() { this.Skin = "皮肤和头发"; this.Limbs = "双手和双脚"; } public void Study() { Console.WriteLine("学习"); } public void Work() { Console.WriteLine("工作"); } } } —————————————————————————————————————————————— namespace e3 { class Program { static void Main(string[] args) { Manmal beast = new Manmal(); //从父类继承来的属性 Console.WriteLine("我是一只野兽!"); Console.WriteLine("我的体温为:{0}",beast.BodyTemparature); Console.WriteLine("我的体重为:{0}",beast.BodyWeight); Console.WriteLine("我有:"); //子类自己定意义的属性 Console.WriteLine(beast.Skin+"\n\n"); Console.WriteLine("我会:"); //从父类继承来的方法 beast.Breathe(); beast.Eat(); beast.Sleep(); //子类自己定义的方法 beast.Run(); beast.Sukle(); Human hm = new Human(); Console.WriteLine("我是一个人类!"); Console.WriteLine("我有:"); Console.WriteLine(hm.Skin); Console.WriteLine(hm.Limbs+"\n\n"); Console.WriteLine("我会:"); hm.Breathe(); beast.Eat(); beast.Sleep(); beast.Run(); beast.Sukle(); hm.Study(); hm.Work(); Console.ReadKey(); } } } —————————————————————————————————————————————————————————————————————————————————————————————— 案例三 namespace e4 { public enum Genders { Male,Female } class Person { public Person(string name, int age, Genders gender) { this.Name = name; this.Age = age; this.Gender = gender; } private string name; public string Name { set { name = value; } get { return name; } } private int age; public int Age { set { age = value; } get { return age; } } private Genders gender; public Genders Gender { set { gender = value; } get { return gender; } } } } ———————————————————————————————————————————— namespace e4 { //C#中继承使用“:”,一旦继承自一个类后意味着当前就拥有了父类的公开成员 class Student:Person { //因为继承了父类一些共性的特征和行为,在子类中我们只需编写子类特有的状态和行为即可! private String hobby; public string Hobby { set { hobby = value; } get { return hobby; } } private int popularity; public int Popularity { set { popularity = value; } get { return popularity; } } public Student(string name, int age, Genders gender, string hobby, int popularity) :base(name,age,gender)//base关键字的运用 继承 { this.Hobby = hobby; this.Popularity = popularity; } public void SayHi() { Console.WriteLine("姓名:{0},年龄:{1},性别:{2},爱好:{3},受欢迎度:{4}",Name,Age,Gender,Hobby,Popularity); } } } —————————————————————————————————————————— namespace e4 { class Teacher:Person { private decimal salary; public decimal Salary { set { salary = value; } get { return salary; } } private int serviceOfYear; public int ServiceOfYear { set { serviceOfYear = value; } get { return serviceOfYear; } } public Teacher(string name, int age, Genders gender, decimal salary, int serviceOfYear) : base(name, age, gender) { this.Salary = salary; this.serviceOfYear = serviceOfYear; } public void SayHi() { Console.WriteLine("姓名:{0},年龄:{1},性别:{2},薪水:{3},工作年限:{4}",Name,Age,Gender,Salary,ServiceOfYear); } } } —————————————————————————————————————— namespace e4 { /// <summary> /// base 关键字的运用! /// </summary> class Program { static void Main(string[] args) { //base关键字就是父类,可以调用父类的成员和构造方法! Student stu = new Student("张三", 18, Genders.Female, "桌球", 101); stu.SayHi(); Teacher tea = new Teacher("李四", 35, Genders.Male, 5000, 5); tea.SayHi(); Console.ReadKey(); } } }