是否可以在Java中扩展非静态内部类?

kly*_*aek 2 java generics inheritance

我需要内部类是非静态的原因是因为我需要内部类来访问它所在的类的泛型.

提前致谢.

emo*_*ory 9

我推测你想从一个封闭的实例外部扩展一个非静态的内部类,这是可能的.

class Alpha
{
      class Beta ( ) { }
}

class Gamma extends Alpha . Beta
{
      // important to get the constructor right or else the whole thing fails
      Gamma ( Alpha alpha )
      {
             alpha . super ( ) ;
      }
}
Run Code Online (Sandbox Code Playgroud)

您还可以在原始封闭类中扩展内部类

class OuterParent
{
     class InnerParent { }

     class InnerChild1 extends OuterParent { }
}
Run Code Online (Sandbox Code Playgroud)

或者扩展原始的封闭类并扩展子类中的内部类

class OuterChild extends OuterParent
{
     class InnerChild2 extends OuterParent { }
}
Run Code Online (Sandbox Code Playgroud)