天天看點

擴充卡模式_設計模式_java_類擴充卡

擴充卡模式:

如同插口一樣:3口和2口如何進行連結呢,需要一個擴充卡進行轉換

1.類擴充卡模式

2.對象擴充卡模式

3.接口擴充卡模式

1.類擴充卡模式

擴充卡模式_設計模式_java_類擴充卡

Voltage220.java

public class Voltage220 {
  public int output220() {
    int src = 220;
    System.out.println("輸入"+src+"伏");
    return src;
  }

}
      

Voltage5.java

public interface Voltage5 {
  public int output5();

}
      

VoltageAdapt.java

public class VoltageAdapt extends Voltage220 implements Voltage5{

  @Override
  public int output5() {
    int src = output220();
    int des = src/44;
    return des;
  }
  

}
      

phone.java

public class phone {
  public void charging(Voltage5 voltage5) {
    if(voltage5.output5() == 5) {
      System.out.println("可以充電了");
    }else if(voltage5.output5() > 5) {
      System.out.println("不可以充電");
    }
  }

}
      
public class Client {

  public static void main(String[] args) {
    // TODO 自動生成的方法存根
    phone p1 = new phone();
    p1.charging(new VoltageAdapt());
    
  }

}