运行这个代码public class Test_03 { public static void main(String[] args) { //员工类:工号\职位\姓名\工资 //经理级:奖金 (奖金和加班费分别用set和get是因为输错,所以单独输入) //后勤:加班费 Manager s1 = new Manager("0001","总经理","张华",5000); s1.setBonus(500);//经理级奖金 s1.work(); Manager s2 = new Manager("0002","副经理","李小",4500); s2.setBonus(350);//经理级奖金 s2.work(); GeneralStaff s3 =new GeneralStaff("1000","后勤主管","叶斯",3300); s3.setOverTimePay(1000);//加班费 s3.work(); GeneralStaff s4 =new GeneralStaff("1001","后勤员工","雷五",2800); s4.setOverTimePay(1000);//加班费 s4.work(); } } class Manager extends Staff {//经理级 public Manager(String id, String position, String name, double salary) { super(id, position, name, salary); } private double bonus;// 奖金 public void setBonus(double bonus) { this.bonus = bonus; } public void work() { super.work(); System.out.println(";奖金:"+ bonus); } } class GeneralStaff extends Staff {// 普通员工类 public GeneralStaff(String id, String position, String name, double salary) { super(id, position, name, salary); // TODO Auto-generated constructor stub } private double overtimepay;// 加班费 public void setOverTimePay(double overtimepay) { this.overtimepay = overtimepay; } public void work() { super.work(); System.out.println(";加班费:"+overtimepay); } } class Staff {// 员工类 private String id;// 工号 private String position;// 职位 private String name;// 姓名 private double salary;// 工资 public Staff(String id, String position, String name, double salary) { super(); this.id = id; this.position = position; this.name = name; this.salary = salary; } public void work() { System.out.print("工号:" + id + ";职位:" + position + ";姓名:" + name + ";工资:" + salary); } }
时间: 2023-06-02 08:08:32 浏览: 257
这段代码是一个员工工资管理系统的实现,包括经理和普通员工两种角色。对于经理级别的员工,还有奖金的设置和输出;对于后勤类的员工,还有加班费的设置和输出。在main函数中,先创建两个经理类和两个后勤类的员工对象,设置相应奖金和加班费后进行工作输出。
相关问题
public class Manager extends Employee { String department; public Manager(int number, String name, String sex, int age, double salary,String department) { super(number, name, sex, age, salary); this.department = department; } public void CalculateSalary() { this.salary = salary*1.5 ; } public void ShowInfo() { System.out.println("工号:"); System.out.println("姓名:"); System.out.println("性别:"); System.out.println("年龄:"); System.out.println("部门:"); System.out.println("工资:"); } }public class Test { public static void main(String[] args) { System.out.println("欢迎进入人事管理系统"); System.out.println("请输入员工工号:"); Worker worker = new Worker(011, "李华", "女", 25, 10000, "程序员"); System.out.println(worker); } },这个Test类怎么样调用前面类中的方法
在Test类中,可以通过创建Manager对象来调用Manager类中的方法,如下所示:
```
public class Test {
public static void main(String[] args) {
System.out.println("欢迎进入人事管理系统");
System.out.println("请输入员工工号:");
Manager manager = new Manager(001, "张三", "男", 35, 20000, "技术部");
manager.CalculateSalary(); // 调用Manager类中的CalculateSalary方法
manager.ShowInfo(); // 调用Manager类中的ShowInfo方法
}
}
```
在上面的代码中,我们创建了一个Manager对象并将它赋值给manager变量。然后我们可以使用这个变量来调用Manager类中的方法。例如,我们调用了CalculateSalary方法来计算经理的工资,并调用ShowInfo方法来显示经理的信息。
import java.util.Scanner; public class Test { public static void main(String[] args) { System.out.println("欢迎进入人事管理系统"); Scanner input = new Scanner(System.in); System.out.println("请输入员工工号:"); String s1 = input.next(); Worker worker = new Worker(011, "李华", "女", 25, 10000, "程序员"); System.out.println(); worker.CalculateSalary(); worker.ShowInfo(); System.out.println(worker); } }找出错误
在创建 Worker 对象时,员工工号应该是字符串类型,而不是整型。因此,应该将 "011" 用双引号括起来,改为 "011"。
另外,在输出 Worker 对象时,应该调用其 toString() 方法,而不是直接输出对象本身。因此,应该将 `System.out.println(worker)` 改为 `System.out.println(worker.toString())` 或者直接省略不写,因为调用 println() 方法时会自动调用对象的 toString() 方法。修改后的代码如下所示:
```
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("欢迎进入人事管理系统");
Scanner input = new Scanner(System.in);
System.out.println("请输入员工工号:");
String s1 = input.next();
Worker worker = new Worker("011", "李华", "女", 25, 10000, "程序员");
System.out.println();
worker.CalculateSalary();
worker.ShowInfo();
System.out.println(worker);
}
}
```
阅读全文