No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation

半城伤御伤魂 提交于 2019-12-03 16:12:59

问题


Consider the below code:

class abstract Normal1 extends Something 
{
}

class Outer 
{
  class abstract Inner extends Normal1
  {

  }
}

class General extends Outer.Inner  // Problem occurs at this 
{
}

The error I am getting is "No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation"

My question is can I extend the inner class like above ?


回答1:


Declare the inner class as static and you should be able to extend it:

class outer {
  static abstract class inner extends normal1 { }
}

If inner is not abstract, it's tied to outer, and can only exist when an instance of outer exists. Check to see if this is what you really want.




回答2:


Nested class are like(in sense ) Property of a class.

  1. As in case of instance variable it only when available when its object is created as same as inner class also available when outer's object will created.

So if you want to extend this then make your inner class as static inner class
As jordao suggest above




回答3:


Try this, (Read nested class inheritance rules).

abstract class normal1 extends something { }

class outer
{
  abstract class  inner extends normal1{}
}

class Outer1 extends outer
{
  class General extends inner {}
}



回答4:


In your class General modify its constructor a to call super inner class constructor. here is the code..

public General(){
    new outer().super();
}


来源:https://stackoverflow.com/questions/8177725/no-enclosing-instance-of-type-perfhelper-is-available-due-to-some-intermediate-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!