Accessing constructor of an anonymous class

后端 未结 10 2090
借酒劲吻你
借酒劲吻你 2020-11-28 03:17

Lets say I have a concrete class Class1 and I am creating an anonymous class out of it.

Object a = new Class1(){
        void someNewMethod(){
        }
             


        
10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 03:35

    It doesn't make any sense to have a named overloaded constructor in an anonymous class, as there would be no way to call it, anyway.

    Depending on what you are actually trying to do, just accessing a final local variable declared outside the class, or using an instance initializer as shown by Arne, might be the best solution.

提交回复
热议问题