java person-employee-manager
时间: 2024-11-03 09:17:16 浏览: 23
在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`。
相关问题
jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company 分数 10 作者 郑如滨 单位 集美大学 定义Person抽象类,Student类、Company类,Employee类。java
这是一个典型的面向对象设计案例,其中包含几个类的层次结构:
1. **Person**:这通常是一个抽象类(abstract class),作为其他三个类的基础。它可能包含一些通用的人的基本属性,如`name`, `age`, 和 `gender`,以及一些通用的行为,比如`eat()` 或 `sleep()`。
```java
public abstract class Person {
private String name;
private int age;
// 构造函数
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 抽象方法
public abstract void work();
// 具体方法
public void eat() {
System.out.println(name + " is eating.");
}
}
```
2. **Student** 和 **Employee**:这两个类都是`Person` 的子类。`Student` 可能添加了特定于学生的属性,如`major`,并重写了`work()` 方法为学习;`Employee` 则可能会有`position`, `salary`等属性,并实现具体的工作行为。
```java
public class Student extends Person {
private String major;
public Student(String name, int age, String major) {
super(name, age);
this.major = major;
}
@Override
public void work() {
System.out.println(name + " is studying.");
}
}
public class Employee extends Person {
private String position;
private double salary;
public Employee(String name, int age, String position, double salary) {
super(name, age);
this.position = position;
this.salary = salary;
}
@Override
public void work() {
System.out.println(name + " is working at a company with position: " + position + ", earning " + salary + ".");
}
}
```
3. **Company**: 这个类可能是另一个实体,可以包含员工列表或其他与公司相关的属性。它可以与`Employee` 类关联,而不是直接继承,因为`Company`不需要所有的`Person`特性。
```java
public class Company {
private List<Employee> employees;
public Company(List<Employee> employees) {
this.employees = employees;
}
public void printEmployees() {
for (Employee employee : employees) {
employee.work();
}
}
}
```
你可以创建一个`Company`实例,然后填充`Employee`列表来演示继承覆盖和多态的应用:
```java
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("John", 30, "Manager", 5000));
employeeList.add(new Student("Alice", 22, "Computer Science"));
Company myCompany = new Company(employeeList);
myCompany.printEmployees();
```
用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}
```
阅读全文