English 中文(简体)
创建普通班级公共内部班级的情况
原标题:Creating instances of public inner classes of generic classes

因此,我有以下想法:

public class Enclosing<T extends Comparable<T>> {
    // non-relevant code snipped
    public class Inner {
        private T value;
        public Inner(T t) {
            value = t;
        }
    }
}

一切汇编,全世界都感到高兴。 然而,每当我试图创建一个<代码>披露的事例时。 内部如下:

new Enclosing<Integer>.Inner(5);

The following error happens:

Cannot distribution the member category Enclosing<Integer>.Inner using a paraileized compounds name; use its summary name and an enclosing instance of category Enclosing<Integer>.

必须指出,我无法使内班<代码>static成为一门,因为它包含一个类型的<代码>。 T。

How can I work around this?

最佳回答

为了瞬间穿透一个内层,你必须首先即刻上外层。 之后,在外物体内与该星号一道制造内物体:

  Enclosing<Integer> outerObject = new Enclosing<Integer>();
  Enclosing<Integer>.Inner innerObject = outerObject.new Inner();

粗金星表示,这一设计中存在一条密码。 可能的话,在保修班(getInner或某种)中应当采用某种工厂方法,如果从保修班外使用,内班可能应当实施公共接口。

问题回答

just ran into the same problem, solved it the following way in java7

public class Test {
    static class A<T> {
        public class B {

        }
    }
    A<String> a = new A();
    class C extends A.B {
        C() {
            Test.this.a.super();
        }
    }
    public void test() {
        C c = new C();
        A.B b = this.a.new B();
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签