1.继承适配类 实现目标接口
package lee.reeves;
//目标接口
public interface DataLine {
void charge();
}
package lee.reeves;
//适配类
public class Usb {
public void usbharge() {
System.out.println("这是USB数据线充电");
}
}
package lee.reeves;
//继承适配对象 实现目标接口来适配
public class UsbTypeCToUsb extends Usb implements DataLine {
@Override
public void charge() {
super.usbharge();
}
public static void main(String[] args) {
UsbTypeCToUsb usbTypeCToUsb=new UsbTypeCToUsb();
usbTypeCToUsb.charge();
}
}
2.组合适配类 实现目标接口
package lee.reeves;
//组合适配对象 实现目标接口来适配
public class UsbTypeCToUsb2 implements DataLine {
private Usb usb;
public UsbTypeCToUsb2(Usb usb){
this.usb=usb;
}
@Override
public void charge() {
usb.usbharge();
}
public static void main(String[] args) {
Usb usb=new Usb();
DataLine dataLine=new UsbTypeCToUsb2(usb);
dataLine.charge();
}
}