适配器
类适配器
public class Voltage220V{
public int output220V(){
int src = 220;
return src;
}
}
public Interface IVoltage5V{
public int output5V();
}
public class VoltageAdapter extends Voltage220V implements IVoltage5V{
public int output5V(){
int srcV = output220V();
int dstV = srcV / 44;
return dstV
}
}
public class Phone{
public void charing(IVoltage5V iVoltage5V){
if(iVoltage5V.output5V==5){
}else if(iVoltage5V.output5V>5){
}
}
}
对象适配器
public class Voltage220V{
public int output220V(){
int src = 220;
return src;
}
}
public Interface IVoltage5V{
public int output5V();
}
public class VoltageAdapter implements IVoltage5V{
private Voltage220 voltage220;
public VoltageAdapter(Voltage220 voltage220){
this.voltage220 = voltage220;
}
public int output5V(){
int dstV = 0;
if(null != voltage220){
int srcV = voltage220.output220V();
dstV = srcV / 44;
}
return dstV
}
}
public class Phone{
public void charing(IVoltage5V iVoltage5V){
if(iVoltage5V.output5V==5){
}else if(iVoltage5V.output5V>5){
}
}
}
接口适配器
public interface Interface{
public void m1();
public void m2();
}
public astract class AbsAdapter implements Interface{
public void m1(){}
public void m2(){}
}
public class Client{
public static main(String[] args){
AbsAdapter absAdapter = new AbsAdapter(){
public void m1(){
}
}
absAdapter.m1();
}
}