描述:某公司中有普通员工、管理人员、秘书等人员,要求设计:雇员类(Employee),字段和属性有员工编号(no)、姓名(name)、性别(gender)、年龄(age)、电话号码(phoneNo),方法是自我介绍(Introduce),输出一行“我是,今年岁”。经理(Manager)类派生自雇员类,属性是经理编号(ManagerNo),除此之外还包含一个发年终奖的方法(YearEndBonus),输出一行“经理发放年终奖了”。秘书类(Secretary)继承自雇员类。在main方法中分别创建Employee类、Manager类、Secretary类的对象,分别为对象赋值,然后输出3个对象的属性,执行3的对象的所有方法。
。学过java这个也会写,都是入门练习题,继承要注意的点是C#一次只允许继承一个类,不能同时继承多个类。
可以看看我们官方文档深入学习一下:https://learn.microsoft.com/zh-cn/dotnet/csharp/fundamentals/tutorials/inheritance
using System;
public class Employee {
public string no;
public string name;
public string gender;
public int age;
public string phoneNo;
public void Introduce() {
Console.WriteLine("我是" + name + ",今年" + age + "岁。");
}
}
public class Manager : Employee {
public string managerNo;
public void YearEndBonus() {
Console.WriteLine("经理发放年终奖了。");
}
}
public class Secretary : Employee {
}
public class Program {
public static void Main() {
Employee emp = new Employee();
emp.no = "001";
emp.name = "张三";
emp.gender = "男";
emp.age = 25;
emp.phoneNo = "123456789";
emp.Introduce();
Manager mgr = new Manager();
mgr.no = "002";
mgr.name = "李四";
mgr.gender = "男";
mgr.age = 35;
mgr.phoneNo = "987654321";
mgr.managerNo = "001";
mgr.Introduce();
mgr.YearEndBonus();
Secretary sec = new Secretary();
sec.no = "003";
sec.name = "王五";
sec.gender = "女";
sec.age = 28;
sec.phoneNo = "456789123";
sec.Introduce();
}
}