JAVA设计模式--七大原则之开闭原则(05)
发布日期:2021-05-10 03:41:47 浏览次数:16 分类:精选文章

本文共 2720 字,大约阅读时间需要 9 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������Java���������������������������������������������������������������CarFactory���

public class Simple01 {
public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
carFactory.getCar(new SuvCar(30));
carFactory.getCar(new JcCar(7));
}
}
class CarFactory {
public void getCar(BaseCar car) {
// ������������������car������������������������������������������������������
// ���������������������������������������������������������������������������������������������������������
car.getCar();
}
}
abstract class BaseCar {
int carType;
abstract void getCar();
}
class SuvCar extends BaseCar {
public SuvCar(int carType) {
super.carType = carType;
}
@Override
void getCar() {
if (carType >= 20) {
System.out.println("������������20������������SUV���");
} else {
System.out.println("���������������������SUV���");
}
}
}

���������������������CarFactory���������getCar������������������BaseCar���������������������������������������SuvCar���������JcCar���CarFactory������car.getCar()���������������getCar���������������������SuvCar���JcCar���������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:JAVA设计模式--七大原则之迪米特法则(最少知道原则)(06)
下一篇:JAVA设计模式--七大原则之里氏替换原则(04)

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年03月31日 23时11分50秒