具有实例变量的泛型内部类,它是另一个泛型内部类的对象。

时间:2021-11-23 22:31:16

First, I realize that I can make ProblemClass a non-generic class and not receive a compiler error. I am wondering what this error means though. My code is very simple, a generic class with two private inner classes. The outer class has an instance variable of type InnerClass<E> and the inner class ProblemClass<E> also has an instance variable of type InnerClass<E>. When ProblemClass` is instantiated, all it does is set o = a, where o,a are the instance variables of the outer class and inner class respectively.

首先,我意识到我可以将问题类变成非泛型类,而不会接收到编译器错误。我想知道这个错误是什么意思。我的代码非常简单,是一个具有两个私有内部类的泛型类。外部类具有类型InnerClass 的实例变量,内部类问题类 也有类型InnerClass 的实例变量。当problem类被实例化时,它所做的就是设置o = a,其中o是外部类和内部类的实例变量。

public class SuperClass<E> {

    private class InnerClass<E> {
    }

    private class ProblemClass<E> {
        private InnerClass<E> o;

        public ProblemClass() {
            o = a;
        }
    }

    private InnerClass<E> a;
}

I receive the following error.

我收到以下错误。

SuperClass.java:11: error: incompatible types
            o = a;
                ^
  required: SuperClass<E#1>.InnerClass<E#2>
  found:    SuperClass<E#1>.InnerClass<E#1>
  where E#1,E#2 are type-variables:
    E#1 extends Object declared in class SuperClass
    E#2 extends Object declared in class SuperClass.ProblemClass
1 error

My question is, what does this error mean?

我的问题是,这个误差意味着什么?

3 个解决方案

#1


4  

Quite simply the E of ProblemClass and the E of InnerClass are not related - your code is equivalent to

很简单,问题类的E和InnerClass的E是无关的——您的代码是等效的。

public class SuperClass<E> {

    private class InnerClass<F> {
    }

    private class ProblemClass<G> {
        private InnerClass<G> o;

        public ProblemClass() {
            o = a;
        }
    }

    private InnerClass<E> a;
}

and you can clearly see that a and o are not necessarily compatible.

你可以清楚地看到a和o不一定是相容的。

#2


1  

As the error is trying to tell you, you have two different Es that may be two different types.

当错误试图告诉你,你有两个不同的Es可能是两种不同的类型。

What would happen if you create a new OuterClass<Integer>.ProblemClass<String>()?

如果创建一个新的OuterClass . problem类 (),会发生什么情况?

#3


0  

@IanRoberts has the answer, some of your E are not the same.

@IanRoberts有答案,你的一些E是不一样的。

Here's a fixed version of your code:

这是你的代码的一个固定版本:

public class SuperClass<SE> {

  private class InnerClass<IE> {
  }

  private class ProblemClass<PE> {
    private InnerClass<SE> o;

    public ProblemClass() {
      o = a;
    }

  }

  private InnerClass<SE> a;
}

#1


4  

Quite simply the E of ProblemClass and the E of InnerClass are not related - your code is equivalent to

很简单,问题类的E和InnerClass的E是无关的——您的代码是等效的。

public class SuperClass<E> {

    private class InnerClass<F> {
    }

    private class ProblemClass<G> {
        private InnerClass<G> o;

        public ProblemClass() {
            o = a;
        }
    }

    private InnerClass<E> a;
}

and you can clearly see that a and o are not necessarily compatible.

你可以清楚地看到a和o不一定是相容的。

#2


1  

As the error is trying to tell you, you have two different Es that may be two different types.

当错误试图告诉你,你有两个不同的Es可能是两种不同的类型。

What would happen if you create a new OuterClass<Integer>.ProblemClass<String>()?

如果创建一个新的OuterClass . problem类 (),会发生什么情况?

#3


0  

@IanRoberts has the answer, some of your E are not the same.

@IanRoberts有答案,你的一些E是不一样的。

Here's a fixed version of your code:

这是你的代码的一个固定版本:

public class SuperClass<SE> {

  private class InnerClass<IE> {
  }

  private class ProblemClass<PE> {
    private InnerClass<SE> o;

    public ProblemClass() {
      o = a;
    }

  }

  private InnerClass<SE> a;
}