Passing “this” in java constructor

后端 未结 9 731
梦毁少年i
梦毁少年i 2020-11-30 11:39

Look into the following code:

public class ClassA {
    private boolean ClassAattr = false;

    public ClassA() {    
        ClassAHandler handler = new Cl         


        
9条回答
  •  野性不改
    2020-11-30 11:46

    there is nothing wrong with the code you pasted, however you can use a non static inner class to make things (arguably) cleaner:

    public class ClassA {
        private boolean ClassAattr = false;
    
        public ClassA() {    
            ClassAHandler handler = new ClassAHandler();
        }
    
        class ClassAHandler extends GeneralHandler {
    
            // magically sees the instantiating ClassA members and methods
        }
    }
    

提交回复
热议问题