简体   繁体   English

使用子类中的静态成员类参数化超类

[英]Parameterizing superclass with static member class from subclass

Is there a way to parameterize a superclass with a static member class of the subclass? 有没有办法用子类的静态成员类参数化超类?

Contrived Example 受挫的例子

ExampleSuperClass.java : ExampleSuperClass.java

package foo;

public class ExampleSuperClass<T> {
    protected T field;

    public ExampleSuperClass(T field) {
        this.field = field;
    }

    public T getField() {
        return field;
    }
}

ExampleSubClass.java : ExampleSubClass.java

package foo;

public class ExampleSubClass extends ExampleSuperClass<Member> {

    static class Member {

    }

    public ExampleSubClass() {
        super(new Member());
    }
}

Compilation fails on ExampleSubClass.java with error: ExampleSubClass.java上的编译失败,错误:

 [javac] ExampleSubClass.java:3: error: cannot find symbol [javac] public class ExampleSubClass extends ExampleSuperClass<Member> { [javac] ^ [javac] symbol: class Member [javac] 1 error 

or in Eclipse with: 或者在Eclipse中:

Member cannot be resolved to a type 成员无法解析为某种类型

in Eclipse the super invocation also has the error: 在Eclipse中, super调用也有错误:

The constructor ExampleSuperClass(Member) refers to missing type Member 构造函数ExampleSuperClass(Member)引用缺少类型的Member


It works fine (aka no errors) if ExampleSubClass is instead parameterized with another package-protected top-level class. 如果使用另一个受包受保护的顶级类参数化ExampleSubClass,它工作正常(也就是没有错误)。


Summary 摘要

The driving force behind this is that I have a generic super class and many different ${SubClass-extends-GenericSuperClass}.java and ${ClassUsedBySubClass}.java pairs. 这背后的驱动力是我有一个通用的超类和许多不同的${SubClass-extends-GenericSuperClass}.java${ClassUsedBySubClass}.java对。 But since ClassUsedBySubClass is only ever referenced by SubClass , it would be nice to: 但由于ClassUsedBySubClass只是被SubClass引用,所以它会很好:

  1. restrict ClassUsedBySubClass 's access by making it a static member class and 通过使ClassUsedBySubClass成为静态成员类来限制它
  2. cut down on the number of files by not giving ClassUsedBySubClass its own file. 通过不给ClassUsedBySubClass提供自己的文件来减少文件数量。

So, is there a way to use a subclass's member class in parameterizing the superclass? 那么,有没有办法在参数化超类时使用子类的成员类?

If there isn't -- is there an alternative approach? 如果没有 - 是否有替代方法?

Yes, you can do it. 是的,你可以做到。 However, since Java uses the scope outside the declaration for name resolution, you must qualify Member with the name of ExampleSubClass : 但是,由于Java使用声明之外的作用域进行名称解析,因此必须使用ExampleSubClass的名称限定Member

public class ExampleSubClass extends ExampleSuperClass<ExampleSubClass.Member> {
    ...
}

Because Member is an inner class, you need to specify that when using it as a generic type. 因为Member是内部类,所以在将其用作泛型类型时需要指定。 I'm able to get it to compile by using the following: 我可以通过使用以下代码来编译它:

static class ExampleSubClass extends  ExampleSuperClass<ExampleSubClass.Member> {
    static class Member {

    }

    public ExampleSubClass() {
        super(new Member());
    }
}

Member is a static nested class. Member是一个静态嵌套类。 Static nested class is accessed using the enclosing class name: 使用封闭的类名访问静态嵌套类:

ExampleSubClass.Member

You do this every time a static nested class occurs outside the scope of its outer class. 每次静态嵌套类出现在其外部类的范围之外时,都会执行此操作。

Therefore, the following is correct: 因此,以下是正确的:

public class ExampleSubClass extends ExampleSuperClass<ExampleSubClass.Member> {
                                                              ^
                                                     enclosing class name
    static class Member {
        ...
    }
}

Same as when you want to create an object for the static nested class: 与要为静态嵌套类创建对象时相同:

ExampleSubClass.Member member = new ExampleSubClass.Member();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM