作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这可能是个愚蠢的问题,但我无法理解为什么下面的编译会失败。
我的类(class)结构
Dao.java
public interface Dao<E extends Entity, S extends SearchCriteria> {
<E> E create(E e) throws Exception;
}
这个 Dao 有一个通用的实现
DaoImpl.java
public abstract class DaoImpl<E extends Entity, S extends SearchCriteria> implements Dao<E, S> {
@Override
public <E> E create(E e) throws Exception {
throw new UnsupportedOperationException("this operation is not supported");
}
}
然后是专门的实现
ProcessDaoImpl.java
public class ProcessDaoImpl extends DaoImpl<Process, WildcardSearchCriteria> {
@Override // this is where compilation is failing, I get the error that create doesn't override a superclass method
public Process create(Process entity) throws Exception {
return null;
}
}
实体类层次结构说明
Process.java
public class Process extends AuditEntity {
// some pojo fields
}
AuditEntity.java
public abstract class AuditEntity extends IdentifiableEntity {
// some pojo fields
}
IdentifiableEntity.java
public abstract class IdentifiableEntity extends Entity {
// some pojo fields
}
Entity.java
public abstract class Entity implements Serializable {
}
最佳答案
因为你应该在接口(interface)和抽象类中声明,E create(E e)
没有 <E>
的方法在开头,否则您不会引用 E
类中声明的类型,但为 E
在方法范围内定义类型:
替换:
public interface Dao<E extends Entity, S extends SearchCriteria> {
<E> E create(E e) throws Exception;
}
由
public interface Dao<E extends Entity, S extends SearchCriteria> {
E create(E e) throws Exception;
}
并替换它:
@Override
public <E> E create(E e) throws Exception {
throw new UnsupportedOperationException("this operation is not supported");
}
作者:
@Override
public E create(E e) throws Exception {
throw new UnsupportedOperationException("this operation is not supported");
}
关于java泛型类层次结构和泛型实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41322977/
我是一名优秀的程序员,十分优秀!