编写一个Java程序,定义员工类,具有姓名、年龄、性别属性,并具有构造方法,显 示数据方法,定义管理层类,继承员工类,并有自己的属性:职务, 年薪。定义职员类,继承员工类,并有自己的属性:所属部门,月 薪。
时间: 2023-09-23 22:06:41 浏览: 151
### 回答1:
以下是Java程序实现:
```java
public class Employee {
private String name;
private int age;
private String gender;
public Employee(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
}
}
public class Management extends Employee {
private String position;
private double annualSalary;
public Management(String name, int age, String gender, String position, double annualSalary) {
super(name, age, gender);
this.position = position;
this.annualSalary = annualSalary;
}
public void display() {
super.display();
System.out.println("Position: " + position);
System.out.println("Annual Salary: " + annualSalary);
}
}
public class Staff extends Employee {
private String department;
private double monthlySalary;
public Staff(String name, int age, String gender, String department, double monthlySalary) {
super(name, age, gender);
this.department = department;
this.monthlySalary = monthlySalary;
}
public void display() {
super.display();
System.out.println("Department: " + department);
System.out.println("Monthly Salary: " + monthlySalary);
}
}
```
在以上代码中,我们定义了一个Employee类,其中包含了员工的基本信息,包括姓名、年龄、性别,并且通过构造方法初始化这些属性,还有一个display()方法用于显示信息。
然后,我们定义了一个Management类和一个Staff类,这两个类都继承了Employee类,并且添加了自己的属性: 管理层类添加了职务和年薪,职员类添加了所属部门和月薪。这两个类也都有一个display()方法用于显示信息。
这里需要注意的是,我们在Management和Staff类中都重写了display()方法,因为我们需要显示员工的所有信息,包括基本信息和自己的特定信息。在这种情况下,我们使用了super关键字来调用父类的display()方法,以便在显示自己的信息之前显示员工的基本信息。
### 回答2:
下面是一个简单的Java程序,实现了员工类、管理层类、职员类的定义。
```java
// 员工类
class Employee {
private String name;
private int age;
private char gender;
public Employee(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void displayData() {
System.out.println("姓名: " + name);
System.out.println("年龄: " + age);
System.out.println("性别: " + gender);
}
}
// 管理层类,继承员工类
class Manager extends Employee {
private String position;
private double annualSalary;
public Manager(String name, int age, char gender, String position, double annualSalary) {
super(name, age, gender);
this.position = position;
this.annualSalary = annualSalary;
}
public void displayData() {
super.displayData();
System.out.println("职务: " + position);
System.out.println("年薪: " + annualSalary);
}
}
// 职员类,继承员工类
class Staff extends Employee {
private String department;
private double monthlySalary;
public Staff(String name, int age, char gender, String department, double monthlySalary) {
super(name, age, gender);
this.department = department;
this.monthlySalary = monthlySalary;
}
public void displayData() {
super.displayData();
System.out.println("所属部门: " + department);
System.out.println("月薪: " + monthlySalary);
}
}
public class Main {
public static void main(String[] args) {
// 创建一个管理层对象
Manager manager = new Manager("张三", 40, '男', "经理", 100000);
manager.displayData();
System.out.println("----------------");
// 创建一个职员对象
Staff staff = new Staff("李四", 25, '女', "销售部", 8000);
staff.displayData();
}
}
```
输出结果如下:
```
姓名: 张三
年龄: 40
性别: 男
职务: 经理
年薪: 100000
----------------
姓名: 李四
年龄: 25
性别: 女
所属部门: 销售部
月薪: 8000
```
### 回答3:
以下是一个简单的示例Java程序,定义了员工类、管理层类和职员类:
```java
public class Employee {
private String name;
private int age;
private String gender;
public Employee(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void displayData() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("性别:" + gender);
}
}
public class Manager extends Employee {
private String position;
private double annualSalary;
public Manager(String name, int age, String gender, String position, double annualSalary) {
super(name, age, gender);
this.position = position;
this.annualSalary = annualSalary;
}
public void displayData() {
super.displayData();
System.out.println("职务:" + position);
System.out.println("年薪:" + annualSalary);
}
}
public class Staff extends Employee {
private String department;
private double monthlySalary;
public Staff(String name, int age, String gender, String department, double monthlySalary) {
super(name, age, gender);
this.department = department;
this.monthlySalary = monthlySalary;
}
public void displayData() {
super.displayData();
System.out.println("所属部门:" + department);
System.out.println("月薪:" + monthlySalary);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee("张三", 25, "男");
System.out.println("员工信息:");
employee.displayData();
Manager manager = new Manager("李四", 30, "女", "经理", 100000);
System.out.println("管理层信息:");
manager.displayData();
Staff staff = new Staff("王五", 28, "男", "销售部", 5000);
System.out.println("职员信息:");
staff.displayData();
}
}
```
运行以上代码,将会输出以下结果:
```
员工信息:
姓名:张三
年龄:25
性别:男
管理层信息:
姓名:李四
年龄:30
性别:女
职务:经理
年薪:100000
职员信息:
姓名:王五
年龄:28
性别:男
所属部门:销售部
月薪:5000
```
阅读全文