模拟一个汽车工厂生产汽车解题提示a)可生产大卡车、大客车、小汽车、跑车等b)不同的车售价不同c)涉及知识点有类、接口、继承、封装、多态提示:运用工厂模式的设计模式去做
public interface ICar {
void price();
}
public class Coach implements ICar {
@Override
public void price() {
System.out.printf("2000万");
}
}
public class Truck implements ICar {
@Override
public void price() {
System.out.printf("1000万\n");
}
}
public class SUV implements ICar {
@Override
public void price() {
System.out.printf("40万");
}
}
public class CarFactory {
public static final int TYPE_TRUCK = 1;
public static final int TYPE_COACH = 2;
public static final int TYPE_SUV = 3;
public ICar createCar(int type) {
if (type == TYPE_COACH) {
return new Coach();
} else if (type == TYPE_TRUCK) {
return new Truck();
} else if (type == TYPE_SUV) {
return new SUV();
} else {
// TODO 其他的车
}
}
}
http://www.sxt.cn/
java第三百集中详细讲了23种设计模式