package com.jerry.design.adapter1.imp;
public interface InterfaceA {
public void testA();
}
接口实现类:
package com.jerry.design.adapter1.impl;
import com.jerry.design.adapter1.imp.InterfaceA;
public class ImplA implements InterfaceA{
@Override
public void testA() {
System.out.println(" i am do something as InterfaceA!");
}
}
目标接口:
package com.jerry.design.adapter1.imp;
public interface InterfaceB {
public void testB();
}
目标接口实现类:
package com.jerry.design.adapter1.impl;
import com.jerry.design.adapter1.imp.InterfaceA;
import com.jerry.design.adapter1.imp.InterfaceB;
public class ImplB implements InterfaceB{
private InterfaceA implA;
public ImplB(InterfaceA implA){
this.implA = implA;
}
@Override
public void testB() {
implA.testA();
}
}
测试方法:
package com.jerry.design.adapter1.client;
import com.jerry.design.adapter1.imp.InterfaceA;
import com.jerry.design.adapter1.imp.InterfaceB;
import com.jerry.design.adapter1.impl.ImplA;
import com.jerry.design.adapter1.impl.ImplB;
public class Test {
public static void main(String[] args) {
InterfaceA implA = (InterfaceA) new ImplA();
InterfaceB implB = (InterfaceB) new ImplB(implA);
implB.testB();// i am do something as InterfaceA!
}
}
总结:原接口转换为目标接口