今日报告
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(); } }
运行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南