1. 设计编写一个控制台应用程序,练习类的继承。
(1) 编写一个抽象类 People,具有”姓名”,”年龄”字段,”姓名”属性,Work 方法。
(2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并
覆盖Work 方法。
(3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。
(4) 在 Student 和 Employer 实例中输出各自不同的信息。
using System; // 抽象类 People abstract class People { public string Name { get; set; } public int Age { get; set; } // 抽象方法 Work public abstract void Work(); } // 学生类 Student 派生自 People 类 class Student : People { public string School { get; set; } // 覆盖Work方法 public override void Work() { Console.WriteLine("我是学生,我的姓名是" + Name + ",年龄是" + Age + ",就读于" + School); } } // 职工类 Employer 派生自 People 类 class Employer : People { public string Company { get; set; } // 覆盖Work方法 public override void Work() { Console.WriteLine("我是职工,我的姓名是" + Name + ",年龄是" + Age + ",就职于" + Company); } } class Program { static void Main() { // 手动输入学生信息 Console.WriteLine("请输入学生信息:"); Console.Write("姓名: "); string studentName = Console.ReadLine(); Console.Write("年龄: "); int studentAge = Convert.ToInt32(Console.ReadLine()); Console.Write("学校: "); string studentSchool = Console.ReadLine(); Student student = new Student { Name = studentName, Age = studentAge, School = studentSchool }; student.Work(); // 手动输入职工信息 Console.WriteLine("\n请输入职工信息:"); Console.Write("姓名: "); string employerName = Console.ReadLine(); Console.Write("年龄: "); int employerAge = Convert.ToInt32(Console.ReadLine()); Console.Write("单位: "); string employerCompany = Console.ReadLine(); Employer employer = new Employer { Name = employerName, Age = employerAge, Company = employerCompany }; employer.Work(); } }
运行结果:
标签:Console,报告,People,Work,Employer,Student,今日,string From: https://www.cnblogs.com/zjq164/p/17883995.html