1、定义Employee员工类: 私有属性:姓名,工号,工资; 公有的构造方法:3个参数,为三个属性赋值; 公有方法: 每个属性的getter和setter方法; print方法,输出员工的所有信息; 2、定义Manager为Employee的子类: 新的私有属性:津贴; 公有构造方法:4个参数,初始化该类的四个属性。 print方法:输出4个属性的值。 3、在Demo类中编写main方法,声明Employee类型的对象p1,为其创建Employee对象,对象的属性值输入;声明Employee类的对象p2,为其创建Manager类对象,属性值输入;分别调用p1和p2的print方法输出各自的信息。
时间: 2024-03-14 10:46:47 浏览: 54
定义一个员工类employee.txt
好的,以下是代码实现:
```
// Employee员工类
public class Employee {
private String name;
private String id;
private double salary;
public Employee(String name, String id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void print() {
System.out.println("姓名:" + name + ",工号:" + id + ",工资:" + salary);
}
}
// Manager经理类,继承自Employee员工类
public class Manager extends Employee {
private double allowance;
public Manager(String name, String id, double salary, double allowance) {
super(name, id, salary);
this.allowance = allowance;
}
public double getAllowance() {
return allowance;
}
public void setAllowance(double allowance) {
this.allowance = allowance;
}
@Override
public void print() {
System.out.println("姓名:" + getName() + ",工号:" + getId() + ",工资:" + getSalary() + ",津贴:" + allowance);
}
}
// Demo类
public class Demo {
public static void main(String[] args) {
// 创建Employee对象并输出信息
Employee p1 = new Employee("张三", "001", 5000);
p1.print();
// 创建Manager对象并输出信息
Manager p2 = new Manager("李四", "002", 8000, 2000);
p2.print();
}
}
```
运行结果:
```
姓名:张三,工号:001,工资:5000.0
姓名:李四,工号:002,工资:8000.0,津贴:2000.0
```
阅读全文