编写测试主类testclass创建employee,manager类的对象并输出该对象各方法运行结果
时间: 2024-05-12 20:18:40 浏览: 80
test_manager
3星 · 编辑精心推荐
好的,您的问题是编写测试主类testclass创建employee,manager类的对象并输出该对象各方法运行结果。下面是示例代码:
```java
public class TestClass {
public static void main(String[] args) {
Employee employee = new Employee("John", 10000);
Manager manager = new Manager("Tom", 20000, 5000);
System.out.println("Employee:");
System.out.println("Name: " + employee.getName());
System.out.println("Salary: " + employee.getSalary());
System.out.println("Annual Salary: " + employee.getAnnualSalary());
System.out.println("\nManager:");
System.out.println("Name: " + manager.getName());
System.out.println("Salary: " + manager.getSalary());
System.out.println("Annual Salary: " + manager.getAnnualSalary());
System.out.println("Bonus: " + manager.getBonus());
}
}
class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public double getAnnualSalary() {
return salary * 12;
}
}
class Manager extends Employee {
private double bonus;
public Manager(String name, double salary, double bonus) {
super(name, salary);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public double getAnnualSalary() {
return super.getAnnualSalary() + bonus;
}
}
```
运行结果如下:
```
Employee:
Name: John
Salary: 10000.0
Annual Salary: 120000.0
Manager:
Name: Tom
Salary: 20000.0
Annual Salary: 260000.0
Bonus: 5000.0
```
阅读全文