简体   繁体   English

如何在静态内部类中重用外部类的泛型?

[英]How to reuse Generic from outer class in static inner class?

It is my understanding that the outer class' parameterized types are accessible to the inner class, possibly this isn't possible using a static inner class? 据我了解,内部类可以访问外部类的参数化类型,使用静态内部类可能无法实现吗?

The below doesn't compile for me. 以下内容不适用于我。 I would like my static inner class to inherit its generic parameters from the wrapper class. 我希望我的静态内部类从包装器类继承其通用参数。

interface CarFactory<V, W> {
   W makeCar(V);
}

public abstract class Car<T, U> {

   ....

   public static class Factory implements CarFactory<T, U> {
    @Override
    public U makeCar(T model) {
      ...
    }
  }
}

From 8.1.2. 8.1.2。开始 Generic Classes and Type Parameters 通用类和类型参数

It is a compile-time error to refer to a type parameter of a generic class C anywhere in: 在以下任何地方引用通用类C类型参数编译时错误

  • the declaration of a static member of C (§8.3.1.1, §8.4.3.2, §8.5.1), or C的静态成员的声明(第8.3.1.1节,第8.4.3.2节,第8.5.1节),或

  • t he declaration of a static member of any type declaration nested within C , or 嵌套在C中的任何类型声明的静态成员的声明 ,或者

  • a static initializer of C (§8.7), or C的静态初始值设定项(第8.7节),或者

  • a static initializer of any class declaration nested within C. 嵌套在C中的任何类声明的静态初始化器。

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

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