java person-employee-manager
时间: 2024-11-03 12:17:16 浏览: 14
在Java中,`Person`, `Employee`, 和 `Manager` 可能是设计模式中的一部分,用于表示一种分层架构。`Person` 类通常作为基础类,表示所有的人。`Employee` 是一个更具体的类,可能包含一些员工特有的属性(如工号、职位等),并且从`Person` 继承。最后,`Manager` 类是一个更特殊的员工,可能是`Employee` 的子类,它可能添加了管理职责相关的特性(如团队、权限等)。
举个例子:
```java
class Person {
String name;
int age;
// 其他通用方法...
}
class Employee extends Person {
int id;
String jobTitle;
// 具体于员工的方法...
}
class Manager extends Employee {
List<Employee> team; // 管理者可能拥有的团队成员
// 专有于经理的方法,如分配任务等...
}
```
在这个结构中,你可以创建`Person` 对象、`Employee` 对象,甚至`Manager` 对象,并通过它们的共通点(比如名字、年龄)进行操作。当需要特定的行为时,例如处理工作任务或管理团队,由于多态性,`Manager` 对象可以自动调用其特有的`Manager` 类的方法,而不需要区分它是`Employee` 还是`Manager`。
相关问题
用java实现五个类,Person,Customer,Employee,Manager,Salesperson.用子类父类联系,并具体实例化
Person类:
```java
public class Person {
private String name;
private int age;
private String address;
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
```
Customer类:
```java
public class Customer extends Person {
private String customerID;
public Customer(String name, int age, String address, String customerID) {
super(name, age, address);
this.customerID = customerID;
}
public String getCustomerID() {
return customerID;
}
public void setCustomerID(String customerID) {
this.customerID = customerID;
}
@Override
public String toString() {
return "Customer{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", address='" + getAddress() + '\'' +
", customerID='" + customerID + '\'' +
'}';
}
}
```
Employee类:
```java
public class Employee extends Person {
private String employeeID;
private double salary;
public Employee(String name, int age, String address, String employeeID, double salary) {
super(name, age, address);
this.employeeID = employeeID;
this.salary = salary;
}
public String getEmployeeID() {
return employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", address='" + getAddress() + '\'' +
", employeeID='" + employeeID + '\'' +
", salary=" + salary +
'}';
}
}
```
Manager类:
```java
public class Manager extends Employee {
private String department;
public Manager(String name, int age, String address, String employeeID, double salary, String department) {
super(name, age, address, employeeID, salary);
this.department = department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Manager{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", address='" + getAddress() + '\'' +
", employeeID='" + getEmployeeID() + '\'' +
", salary=" + getSalary() +
", department='" + department + '\'' +
'}';
}
}
```
Salesperson类:
```java
public class Salesperson extends Employee {
private double commissionRate;
public Salesperson(String name, int age, String address, String employeeID, double salary, double commissionRate) {
super(name, age, address, employeeID, salary);
this.commissionRate = commissionRate;
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
@Override
public String toString() {
return "Salesperson{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", address='" + getAddress() + '\'' +
", employeeID='" + getEmployeeID() + '\'' +
", salary=" + getSalary() +
", commissionRate=" + commissionRate +
'}';
}
}
```
具体实例化:
```java
public class Main {
public static void main(String[] args) {
Customer customer = new Customer("张三", 18, "北京市", "0001");
System.out.println(customer);
Employee employee = new Employee("李四", 25, "上海市", "0002", 5000);
System.out.println(employee);
Manager manager = new Manager("王五", 30, "广州市", "0003", 8000, "销售部");
System.out.println(manager);
Salesperson salesperson = new Salesperson("赵六", 28, "深圳市", "0004", 6000, 0.1);
System.out.println(salesperson);
}
}
```
输出:
```
Customer{name='张三', age=18, address='北京市', customerID='0001'}
Employee{name='李四', age=25, address='上海市', employeeID='0002', salary=5000.0}
Manager{name='王五', age=30, address='广州市', employeeID='0003', salary=8000.0, department='销售部'}
Salesperson{name='赵六', age=28, address='深圳市', employeeID='0004', salary=6000.0, commissionRate=0.1}
```
使用java实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一个方法add用于涨工资,普通员工一次能涨10%,经理能涨20%。具体要求如下:\n(1)Person类中的属性有:姓名name、地址address,定义该类的构造方法; \n(2)Employee类中的属性有:工号id、工资wage、工龄workAge,定义该类的构造方法; \n(3)Manager类中的属性有:级别level,定义该类的构造方法; \n(4)编写一个测试类,创建一个员工和一个经理对象,给该员工和经理涨工资,并输出其具有的信息。
下面是Java代码实现:
```java
// Person类
public class Person {
private String name;
private String address;
public Person(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
}
// Employee类
public class Employee extends Person {
private int id;
private double wage;
private int workAge;
public Employee(int id, double wage, int workAge, String name, String address) {
super(name, address);
this.id = id;
this.wage = wage;
this.workAge = workAge;
}
// 涨工资方法
public void add() {
wage *= 1.1;
}
public int getId() {
return id;
}
public double getWage() {
return wage;
}
public int getWorkAge() {
return workAge;
}
}
// Manager类
public class Manager extends Employee {
private String level;
public Manager(String level, int id, double wage, int workAge, String name, String address) {
super(id, wage, workAge, name, address);
this.level = level;
}
// 涨工资方法
public void add() {
wage *= 1.2;
}
public String getLevel() {
return level;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Employee emp = new Employee(1001, 5000, 3, "张三", "北京市");
Manager mgr = new Manager("部门经理", 1002, 8000, 5, "李四", "上海市");
// 涨工资
emp.add();
mgr.add();
// 输出信息
System.out.println(emp.getName() + "的工资为:" + emp.getWage());
System.out.println(mgr.getName() + "的工资为:" + mgr.getWage());
}
}
```
运行结果如下:
```
张三的工资为:5500.0
李四的工资为:9600.0
```
可以看到,普通员工涨了10%,经理涨了20%。
阅读全文