我用参数化类型得到一个奇怪的行为。 我是一个扩展类B的类A.A具有扩展类Predicate的内部类A1。 的代码看起来是这样的:参数化类问题
public abstract class A<T> extends B<T> {
public T uniqueResult (String param, Object value) {
A1 filter = new A1();
filter.setParam(param);
filter.setValue(value);
return container.query(filter).next();
}
public class A1 extends Predicate<T> {
public boolean match (T bean) {
Object result = BeanUtils.getPropertyValue(bean, fieldName);
return result == null ? null : result.equals(value);
}
}
}
最后类d延伸的,并使用uniqueResult方法:
public class D extends A<MyVO> {
public MyVO findById (BigDecimal id) {
return uniqueResult("id", id);
}
}
的问题是,所述匹配方法(内侧A1)接收的代替MyVO BigDecimal的。 如果我重写D中的uniqueResult方法,它工作正常,但我不明白为什么会发生这种情况。
的overrided uniqueResult看起来是这样的:
public MyVO uniqueResult (final String fieldName, final Object value) {
return container().query(
new Predicate<MyVO>() {
private static final long serialVersionUID = 1L;
public boolean match(MyVO bean) {
Object result = BeanUtils.getPropertyValue(bean, fieldName);
return result == null ? null : result.equals(value);
}
}
).next();
}
任何想法?
'最后是班级,' - 班级在哪里?它看起来像你错过了这个问题... – Krease
对不起,似乎有一部分是错过了。 –
根据你如何定义'D','uniqueResult'应该返回'MyVO'类型 - 所有其他事物(B,A1)不影响'uniqueResult'的返回类型。 – Krease