增加普通客户和VIP客户,并且添加地址信息和生日信息
时间: 2024-09-12 09:14:37 浏览: 56
为了增加普通客户和VIP客户,我们可以对`Customer`类进行扩展,创建两个子类`NormalCustomer`和`VIPCustomer`。同时,我们添加地址信息`address`和生日信息`birthday`。以下是修改后的代码:
```java
public abstract class Customer {
// 基本信息
private String name;
private int age;
// 地址信息
private String address;
// 生日信息
private LocalDate birthday;
// 构造函数
public Customer(String name, int age, String address, LocalDate birthday) {
this.name = name;
this.age = age;
this.address = address;
this.birthday = birthday;
}
// 抽象方法
public abstract void getInformation();
// 共享的方法
public void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address: " + address);
System.out.println("Birthday: " + birthday);
getInformation();
}
}
// 普通客户类
public class NormalCustomer extends Customer {
// 重写getInformation()
@Override
public void getInformation() {
System.out.println("This is a normal customer.");
}
}
// VIP客户类
public class VIPCustomer extends Customer {
// 重写getInformation()
@Override
public void getInformation() {
System.out.println("This is a VIP customer with additional privileges.");
}
}
```
现在,`NormalCustomer`和`VIPCustomer`都有了地址和生日信息,并且都实现了`getInformation()`方法,以显示其各自的特点。
阅读全文