我试图将请求的基本实现作为接口函数的参数类型。
然后让各种类扩展基本请求类以赋予请求对象更具体的含义。
但是 Java 需要确切的类作为参数,并且不尊重由该基类扩展的类。
public interface MyInterface{
public String getValue(BaseRequest req);
}
public class MyInterfaceImpl implements MyInterface{
public String getValue(SpecificRequest req){ //Java will give a compile error here.
//Impl
}
}
public interface BaseRequest{
int requiredA;
int requiredB;
//setter and getters for A and B
}
public class SpecificRequest extends BaseRequest{
int specificValC;
//setter and getters for C
}
实现这种模式的最佳方法是什么?还是我设计的有点太多了?