Java 继承:具有相同绑定的不兼容泛型类型
因此,我有一个Java 继承:具有相同绑定的不兼容泛型类型,java,generics,inheritance,Java,Generics,Inheritance,因此,我有一个public接口NodeLevel,它有方法public E getParent()和public E getEnum()。实现的抽象公共类NodeLevelAbstract实现了NodeLevel委托给实现公共接口NodeLevelEnum的类的实例。最后一个声明应该意味着实现NodeLevelEnum的类的实例可以对实现NodeLevelEnum的类的实例做一些事情 现在,这是NodeLevelAbstract#getParent(): 导致编译器错误(我使用NetBeans
public接口NodeLevel
,它有方法public E getParent()
和public E getEnum()
。实现的抽象公共类NodeLevelAbstract实现了NodeLevel
委托给实现公共接口NodeLevelEnum
的类的实例。最后一个声明应该意味着实现NodeLevelEnum
的类的实例可以对实现NodeLevelEnum
的类的实例做一些事情
现在,这是NodeLevelAbstract#getParent()
:
导致编译器错误(我使用NetBeans 7.2,顺便说一句):
对我来说,这意味着需要一个NodeLevelEnum
的后代,但是找到了NodeLevelEnum
,考虑到Java泛型中的绑定本身,这听起来像是绝对的胡说八道
为什么这些类型不兼容?还有什么优雅的方式来实现我的目标吗?
提前感谢。=)
更新1
顺便说一句,NodeLevelEnum#getParent()
返回
,而不是NodeLevelEnum
,错误显示返回
更新2
abstract public class NodeLevelAbstract<E extends NodeLevelEnum> implements
NodeLevel<E> {
protected E _enum;
@Override
public E getEnum() {
return this._enum;
}
@Override
public E getParent() {
return this.getEnum().getParent();
}
public static <E extends NodeLevelEnum<E>> E[] getEnumLineage(E _enum) {
ArrayList<E> ancestors = new ArrayList<>();
E currentEnum = _enum;
do {
ancestors.add(currentEnum);
currentEnum = currentEnum.getParent();
} while (currentEnum != null);
return (E[]) ancestors.toArray();
}
public static <E extends NodeLevelEnum<E>> HashMap<String, String>
getEnumLineageValueMap(
E _enum) {
HashMap<String, String> map = new HashMap<>();
for (E e : getEnumLineage(_enum)) {
map.put(e.getCode(), e.getValue());
}
return map;
}
}
public interface NodeLevel<E extends NodeLevelEnum> {
public E getEnum();
public E getParent();
}
public interface NodeLevelEnum<E extends NodeLevelEnum> {
public E getParent();
}
public interface FilestructureLevel<E extends NodeLevelEnum<E>> extends
NodeLevel<E> {
public String getPathPrefix();
}
public class FileLevel<E extends NodeLevelEnum<E>> extends NodeLevelAbstract<E>
implements FilestructureLevel<E> {
protected String _pathPrefix;
@Override
public String getPathPrefix() {
return this._pathPrefix;
}
public HashMap<String, String> getValueMap(Boolean withPath) {
return getEnumLineageValueMap(this.getEnum(), withPath);
}
public static <E extends NodeLevelEnum<E>> HashMap<String, String>
getEnumLineageValueMap(
E _enum) {
return getEnumLineageValueMap(_enum, false);
}
public static <E extends NodeLevelEnum<E>> HashMap<String, String>
getEnumLineageValueMap(
E _enum, Boolean withPath) {
HashMap<String, String> map = new HashMap<>();
FileLevelEnum[] lineage = (FileLevelEnum[]) getEnumLineage(_enum);
for (FileLevelEnum e : lineage) {
String value = !withPath ? e.getValue() : e.getPathPrefix()
+ e.getValue();
map.put(e.getCode(), value);
}
return map;
}
}
抽象公共类NodeLevel抽象实现
NodeLevel{
受保护的E_enum;
@凌驾
公共E getEnum(){
返回此。\u enum;
}
@凌驾
公共E getParent(){
返回此.getEnum().getParent();
}
公共静态E[]GetEnumLegene(E _enum){
ArrayList祖先=新的ArrayList();
E currentEnum=_enum;
做{
添加(当前枚举);
currentEnum=currentEnum.getParent();
}while(currentEnum!=null);
返回(E[])个祖先。toArray();
}
公共静态哈希映射
GetEnumRiegeValueMap(
E_enum){
HashMap=newHashMap();
对于(E:GetEnumLegence(_enum)){
put(e.getCode(),e.getValue());
}
返回图;
}
}
公共接口节点级{
公共E getEnum();
公共E getParent();
}
公共接口NodeLevelEnum{
公共E getParent();
}
公共接口文件结构级别扩展
NodeLevel{
公共字符串getPathPrefix();
}
公共类文件级扩展了NodeLevel抽象
实现文件结构级别{
受保护的字符串\u路径前缀;
@凌驾
公共字符串getPathPrefix(){
返回此。\u路径前缀;
}
公共HashMap getValueMap(带路径的布尔值){
返回GetEnumReliegentValueMap(this.getEnum(),withPath);
}
公共静态哈希映射
GetEnumRiegeValueMap(
E_enum){
返回GetEnumSeregeValueMap(_enum,false);
}
公共静态哈希映射
GetEnumRiegeValueMap(
E_枚举,带路径的布尔值){
HashMap=newHashMap();
FileLevelEnum[]沿袭=(FileLevelEnum[])GetEnum沿袭(_enum);
对于(FileLevelEnum e:沿袭){
字符串值=!withPath?e.getValue():e.getPathPrefix()
+e.getValue();
put(如getCode(),value);
}
返回图;
}
}
那么您是否尝试过将NodeLevel
更改为NodeLevel
?如果您只是发布了接口和类的(最低)版本,而不是试图用散文来描述它们,那么您的问题会更容易阅读!你的第一句话奏效了。你能解释一下吗?@XedinUnknown-看问题。@XedinUnknown-特别好。
incompatible types
required: E
found: NodeLevelEnum
where E is a type-variable:
E extends NodeLevelEnum declared in class NodeLevelAbstract
abstract public class NodeLevelAbstract<E extends NodeLevelEnum> implements
NodeLevel<E> {
protected E _enum;
@Override
public E getEnum() {
return this._enum;
}
@Override
public E getParent() {
return this.getEnum().getParent();
}
public static <E extends NodeLevelEnum<E>> E[] getEnumLineage(E _enum) {
ArrayList<E> ancestors = new ArrayList<>();
E currentEnum = _enum;
do {
ancestors.add(currentEnum);
currentEnum = currentEnum.getParent();
} while (currentEnum != null);
return (E[]) ancestors.toArray();
}
public static <E extends NodeLevelEnum<E>> HashMap<String, String>
getEnumLineageValueMap(
E _enum) {
HashMap<String, String> map = new HashMap<>();
for (E e : getEnumLineage(_enum)) {
map.put(e.getCode(), e.getValue());
}
return map;
}
}
public interface NodeLevel<E extends NodeLevelEnum> {
public E getEnum();
public E getParent();
}
public interface NodeLevelEnum<E extends NodeLevelEnum> {
public E getParent();
}
public interface FilestructureLevel<E extends NodeLevelEnum<E>> extends
NodeLevel<E> {
public String getPathPrefix();
}
public class FileLevel<E extends NodeLevelEnum<E>> extends NodeLevelAbstract<E>
implements FilestructureLevel<E> {
protected String _pathPrefix;
@Override
public String getPathPrefix() {
return this._pathPrefix;
}
public HashMap<String, String> getValueMap(Boolean withPath) {
return getEnumLineageValueMap(this.getEnum(), withPath);
}
public static <E extends NodeLevelEnum<E>> HashMap<String, String>
getEnumLineageValueMap(
E _enum) {
return getEnumLineageValueMap(_enum, false);
}
public static <E extends NodeLevelEnum<E>> HashMap<String, String>
getEnumLineageValueMap(
E _enum, Boolean withPath) {
HashMap<String, String> map = new HashMap<>();
FileLevelEnum[] lineage = (FileLevelEnum[]) getEnumLineage(_enum);
for (FileLevelEnum e : lineage) {
String value = !withPath ? e.getValue() : e.getPathPrefix()
+ e.getValue();
map.put(e.getCode(), value);
}
return map;
}
}