java泛型类层次结构和泛型实现
发布时间:2020-12-15 04:37:08 所属栏目:Java 来源:网络整理
导读:这可能是一个愚蠢的问题,但我无法理解为什么下面的编译失败了. 我的班级层次结构 Dao.javapublic interface DaoE extends Entity,S extends SearchCriteria { E E create(E e) throws Exception;} 这个Dao有一个通用的实现 DaoImpl.javapublic abstract clas
|
这可能是一个愚蠢的问题,但我无法理解为什么下面的编译失败了.
我的班级层次结构 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 {
}
解决方法
因为你应该在接口和抽象类中声明,没有< E>的E create(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");
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |








