用java实现五个类,Person,Customer,Employee,Manager,Salesperson.用子类父类联系,并具体实例化
时间: 2024-05-02 20:17:50 浏览: 92
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}
```
阅读全文