Accessing constructor of an anonymous class

后端 未结 10 2091
借酒劲吻你
借酒劲吻你 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:52

    I know the thread is too old to post an answer. But still i think it is worth it.

    Though you can't have an explicit constructor, if your intention is to call the constructor of the super class, then the following is all you have to do.

    StoredProcedure sp = new StoredProcedure(datasource, spName) {
        {// init code if there are any}
    };
    

    This is an example of creating a StoredProcedure object in Spring by passing a DataSource and a String object.

    So the Bottom line is, if you want to create an anonymous class and want to call the super class constructor then create the anonymous class with a signature matching the super class constructor.

提交回复
热议问题