Java - '名称冲突'和'构造函数undefined'覆盖从外部类继承泛型的内部类时出错

时间:2018-05-29 09:04:16

标签: java eclipse name-clash

我最近在项目中升级了eclipse和java版本,但发生了一些错误。 我尝试重现并发现以下设置导致使用Java 1.8的Eclipse Oxigen.3a(4.7.3a)中的错误,而同样适用于eclipse 3.7.2和Java 1.7

TestSomeObject.java:

package a;
public class TestSomeObject
{
}

TestGeneric.java:

package a;
public class TestGeneric<T>
{
  T element;

  protected class InnerGeneric
  {
     T innerElement;

     public InnerGeneric() { }
  }
}

TestA.java:

package a;

public class TestA extends TestGeneric<TestSomeObject>
{
  public TestA(String a, String b) {}

  public InnerA someMethod(String some, InnerGeneric inner)
  {
     return new InnerA(some, inner);
  }

  protected class InnerA
  {
     public InnerA(String a, InnerGeneric b) {  }
  }
}

TestB.java

package a;
import a.TestA;

public class TestB extends TestA
{
  public TestB(String a, String b){
     super(a,b);
  }

  public InnerA someMethod(String some, InnerGeneric inner)
  {
     return new InnerB(some, inner);
  }

  protected class InnerB extends InnerA
  {
     public InnerB(String a, InnerGeneric b)
     {
        super(a, b);
     }
  }
}

在类测试B中,我收到以下编译错误:

  1. someMethod:Description Resource Path Location Type Name clash: The method someMethod(String, TestGeneric<TestSomeObject>.InnerGeneric) of type TestB has the same erasure as someMethod(String, TestGeneric<TestSomeObject>.InnerGeneric) of type TestA but does not override it TestB.java line 13 Java Problem
  2. 在致电超级(a,b)时:Description Resource Path Location Type The constructor TestA.InnerA(String, TestGeneric<TestSomeObject>.InnerGeneric) is undefined TestB.java line 22在此处输入代码Java Problem
  3. 在eclipse版本:3.7.2和Java 1.7中,这可行。

    为什么?我该怎么做才能清除错误?

1 个答案:

答案 0 :(得分:1)

答案是,更精确地定义方法签名中的InnerGeneric类:

TestB.java:

package a;

public class TestB extends TestA
{
  public TestB(String a, String b){
     super(a,b);
  }

  public InnerA someMethod(String some, TestGeneric<TestSomeObject>.InnerGeneric inner)
  {
     return new InnerB(some, inner);
  }

  protected class InnerB extends InnerA
  {
     public InnerB(String a, TestGeneric<TestSomeObject>.InnerGeneric b)
     {
        super(a, b);
     }
  }
}