Accessing constructor of an anonymous class

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

    Here's another way around the problem:

    public class Test{
    
        public static final void main(String...args){
    
            new Thread(){
    
                private String message = null;
    
                Thread initialise(String message){
    
                    this.message = message;
                    return this;
                }
    
                public void run(){
                    System.out.println(message);
                }
            }.initialise(args[0]).start();
        }
    }
    

提交回复
热议问题