标签:null shutdown phone return erp void sys str art
车的接口
public interface Car {
void name();
}
车接口的三个实现类
public class BYD implements Car{
@Override
public void name() {
System.out.println("比亚迪!");
}
}
public class NIO implements Car{
@Override
public void name() {
System.out.println("蔚来!");
}
}
public class Tesla implements Car{
@Override
public void name() {
System.out.println("特斯拉!");
}
}
车工厂类
public class CarFactory {
public static Car getCar(String car){
if (car.equals("比亚迪")) {
return new BYD();
}else if(car.equals("特斯拉")){
return new Tesla();
}else if(car.equals("蔚来")){
return new NIO();
}
else {
return null;
}
}
}
程序入口
public class Me {
public static void main(String[] args) {
Car car = CarFactory.getCar("比亚迪");
Car car2 = CarFactory.getCar("特斯拉");
Car car3 = CarFactory.getCar("蔚来");
car.name();
car2.name();
car3.name();
}
}
运行结果
特斯拉!
蔚来!
比亚迪!
车的接口
public interface Car {
void name();
}
车接口的三个实现类
public class BYD implements Car{
@Override
public void name() {
System.out.println("比亚迪!");
}
}
public class NIO implements Car{
@Override
public void name() {
System.out.println("蔚来!");
}
}
public class Tesla implements Car{
@Override
public void name() {
System.out.println("特斯拉!");
}
}
车工厂接口
public interface CarFactory {
Car getCar();
}
车工厂接口的三个实现类
public class BYDFactory implements CarFactory{
@Override
public Car getCar() {
return new BYD();
}
}
public class NIOFactory implements CarFactory{
@Override
public Car getCar() {
return new NIO();
}
}
public class TeslaFactory implements CarFactory {
@Override
public Car getCar() {
return new Tesla();
}
}
程序入口
public class Me {
public static void main(String[] args) {
Car car1 = new TeslaFactory().getCar();
Car car2 = new NIOFactory().getCar();
Car car3 = new BYDFactory().getCar();
car1.name();
car2.name();
car3.name();
}
}
运行结果
特斯拉!
蔚来!
比亚迪!
手机产品接口
public interface PhoneProduct {
void start();
void shutdown();
void callup();
void sendSMS();
}
路由器产品接口
public interface RouterProduct {
void start();
void shutdown();
void openWifi();
void setting();
}
手机产品接口的两个实现类
public class XiaomiPhone implements PhoneProduct {
public void start() {
System.out.println("开启小米手机");
}
@Override
public void shutdown() {
System.out.println("关闭小米手机");
}
@Override
public void callup() {
System.out.println("小米手机打电话");
}
@Override
public void sendSMS() {
System.out.println("小米手机发短信");
}
}
public class HuaweiPhone implements PhoneProduct {
@Override
public void start() {
System.out.println("开启华为手机");
}
@Override
public void shutdown() {
System.out.println("关闭华为手机");
}
@Override
public void callup() {
System.out.println("华为手机打电话");
}
@Override
public void sendSMS() {
System.out.println("华为手机发短信");
}
}
路由器产品的两个实现类
public class XiaomiRouter implements RouterProduct {
@Override
public void start() {
System.out.println("小米路由器启动");
}
@Override
public void shutdown() {
System.out.println("小米路由器关闭");
}
@Override
public void openWifi() {
System.out.println("小米路由器开启WiFi");
}
@Override
public void setting() {
System.out.println("设置小米路由器");
}
}
public class HuaweiRouter implements RouterProduct {
@Override
public void start() {
System.out.println("华为路由器启动");
}
@Override
public void shutdown() {
System.out.println("华为路由器关闭");
}
@Override
public void openWifi() {
System.out.println("华为路由器开启WiFi");
}
@Override
public void setting() {
System.out.println("设置华为路由器");
}
}
产品工厂接口
public interface ProductFactory {
//生产手机
PhoneProduct iphoneProduct();
//生产路由器
RouterProduct routerProduct();
}
产品工厂接口的两个实现类
public class XiaomiFactory implements ProductFactory {
@Override
public PhoneProduct iphoneProduct() {
return new XiaomiPhone();
}
@Override
public RouterProduct routerProduct() {
return new XiaomiRouter();
}
}
public class HuaiweiFactory implements ProductFactory {
@Override
public PhoneProduct iphoneProduct() {
return new HuaweiPhone();
}
@Override
public RouterProduct routerProduct() {
return new HuaweiRouter();
}
}
程序入口
public class Client {
public static void main(String[] args) {
System.out.println("-------------小米------------------");
//小米工厂
XiaomiFactory xiaomiFactory = new XiaomiFactory();
//生产小米手机
PhoneProduct xiaomiPhone = xiaomiFactory.iphoneProduct();
xiaomiPhone.callup();
xiaomiPhone.sendSMS();
//生产小米路由器
RouterProduct xiaomiRouter = xiaomiFactory.routerProduct();
xiaomiRouter.openWifi();
xiaomiRouter.setting();
System.out.println("-------------华为------------------");
HuaiweiFactory huaiweiFactory = new HuaiweiFactory();
PhoneProduct huaweiPhone = huaiweiFactory.iphoneProduct();
huaweiPhone.callup();
huaweiPhone.start();
RouterProduct huaweiRouter = huaiweiFactory.routerProduct();
huaweiRouter.setting();
huaweiRouter.shutdown();
}
}
运行结果
-------------小米------------------
小米手机打电话
小米手机发短信
小米路由器开启WiFi
设置小米路由器
-------------华为------------------
华为手机打电话
开启华为手机
设置华为路由器
华为路由器关闭
标签:null shutdown phone return erp void sys str art
原文地址:https://www.cnblogs.com/baihan/p/12996969.html