一个员工类中包含,员工姓名,年龄,工资等参数,经理类继承员工类,比员工多出奖金参数,要求从健盘输入经理的姓名,年龄,工资,奖金信息,然后只打印出姓名与奖金的信息,使用java语言编写 输入:owen 10000 25 60000 输出: Name :owen Bonus: 60000
时间: 2024-03-06 11:49:48 浏览: 114
好的,这是一个简单的Java继承实现,可以参考以下代码:
```
import java.util.Scanner;
class Employee {
private String name;
private int age;
private double salary;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
}
class Manager extends Employee {
private double bonus;
public void setBonus(double bonus) {
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void printInfo() {
System.out.println("Name: " + getName());
System.out.println("Bonus: " + getBonus());
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Manager manager = new Manager();
System.out.print("Input name: ");
manager.setName(scanner.nextLine());
System.out.print("Input age: ");
manager.setAge(scanner.nextInt());
System.out.print("Input salary: ");
manager.setSalary(scanner.nextDouble());
System.out.print("Input bonus: ");
manager.setBonus(scanner.nextDouble());
manager.printInfo();
}
}
```
当你输入 "owen 10000 25 60000" 时,程序会输出:
```
Input name: owen
Input age: 25
Input salary: 10000
Input bonus: 60000
Name: owen
Bonus: 60000.0
```
请注意,这个程序只会打印经理的姓名和奖金。如果你想打印其他信息,可以在 `printInfo()` 方法中添加更多代码。
阅读全文