为什么编译器声明不存在唯一的最大实例? [英] Why does the compiler state no unique maximal instance exists?

查看:43
本文介绍了为什么编译器声明不存在唯一的最大实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

public class Obj<T> extends BaseModel {

    public static final String OBJECT = "object";

    public Obj(T object) {
        setObject(object);
    }

    public T getObject() {
        return get(OBJECT);
    }

    public void setObject(T object) {
        set(OBJECT, object);
    }
}

还有……

/** This is a 3rd party library class **/
public class BaseModel implements ModelData, Serializable {
  //...members and stuff...

  @SuppressWarnings({"unchecked", "rawtypes"})
  public <X> X get(String property) {
    X obj = null;
    if (start > -1 && end > -1) {
      Object o = map.get(property.substring(0, start));
      String p = property.substring(start + 1, end);
      if (o instanceof Object[]) {
        obj = (X) ((Object[]) o)[Integer.valueOf(p)];
      } else if (o instanceof List) {
        obj = (X) ((List) o).get(Integer.valueOf(p));
      } else if (o instanceof Map) {
        obj = (X) ((Map) o).get(p);
      }
    } else {
      obj = (X) map.get(property);
    }
    return obj;
  }
}

编译时出现以下错误.

X的类型参数无法确定;对于具有上限 T,java.lang.Object -> 的类型变量 X 不存在唯一的最大实例getObject()

它不会发生在 Eclipse 中,据我所知,它使用与我的 Ant 构建相同的 JDK.我看过关于Sun编译器问题的SO线程,但是这似乎是用于动态声明类型的静态方法.

It doesn't happen in Eclipse, which, as far as I can tell, is using the same JDK as my Ant build. I've seen the SO thread about the Sun compiler issue, but that seemed to be for static methods declaring types on the fly.

为什么我会收到此错误,更重要的是,我该如何解决?

Why am I getting this error, and more importantly, how do I get around it?

到目前为止,我发现的唯一原因是像这样在我的方法中进行转换:

So far the only why I've found is to cast in my method like this:

@SuppressWarnings({"unchecked"})
public T getObject() {
    return (T) get(OBJECT); //yuck
}

告诉我我正在破解,这是可以接受的正确方法.

Telling my I'm on crack and this is the proper way is acceptable.

推荐答案

它无法编译,因为您的代码对泛型期望太多 -> 即 <X > X 参与:

It does not compile because your code expects too much from generics -> i.e., the < X > X part in:

public <X> X get(String property) { ... }

在以下代码中:

public T getObject() {
  return get(OBJECT);
}

您必须记住,在编译器实际开始编译 Java 代码之前,泛型总是展开".这是一个预处理步骤.

you have to keep in mind that generics are always "unfolded" before the compiler actually starts to compile the Java code. It is a pre-processing step.

在您的情况下,编译器不知道用什么来替换 X 在编译时.编译器需要确定 X 的类型,因为它需要对照 T 来检查它以验证代码.因此错误...

In your case, the compiler does not know what to use to replace X at compile time. The compiler needs to be sure about the type of X, because it needs to check it against T to validate the code. Hence the error...

解决您的问题的方法是替换 <X > X 带对象:

A solution to your issue is to replace < X > X with Object:

public Object get(String property) { ... }

并添加演员表:

public T getObject() {
  return (T) get(OBJECT);
}

您将在编译时收到 unchecked-cast 警告,但您的代码会编译(所以您的解决方法是有效的).

Your will get an unchecked-cast warning at compile time, but your code will compile (so yes your workaround is valid).

这篇关于为什么编译器声明不存在唯一的最大实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆