1 回答
![?](http://img1.sycdn.imooc.com/545865890001495702200220-100-100.jpg)
TA贡献2036条经验 获得超8个赞
这是一个著名的模式适配器。代码看起来像这样:
class GenericRequest {}
class SpecificRequest1 extends GenericRequest {
void method1() {
System.out.println("specific1");
}
}
class SpecificRequest2 extends GenericRequest {
void method2() {
System.out.println("specific2");
}
}
interface ReqInterface {
void method();
}
class Specific1 implements ReqInterface {
private final SpecificRequest1 req =new SpecificRequest1();
@Override
public void method() {
req.method1();
}
}
class Specific2 implements ReqInterface {
private final SpecificRequest2 req =new SpecificRequest2();
@Override
public void method() {
req.method2();
}
}
public class Production {
void method(ReqInterface req) {
req.method();
}
public static void main(String[] args) {
Production l3 = new Production();
l3.method(new Specific1());
l3.method(new Specific2());
}
}
尝试避免任何方法参数中的布尔值,instanceof不惜一切代价))
添加回答
举报