java--内部类中.this与.new用法

.this   生成对外部类的引用

public class DotThis {
    void f(){
        System.out.println("DotThis.f()");
    }
    public class Inner{
        public DotThis outer(){
            return DotThis.this;
        }
    }
    public Inner inner(){
        return new Inner();
    }
    public static void main(String [] args){
        DotThis dt = new DotThis();
        DotThis.Inner dti = dt.inner();
        dti.outer().f();
    }

}

 运行结果

DotThis.f()

.new  创建某个内部类的对象

public class DotNew {
    public class Inner{
        
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DotNew  dn = new DotNew();
        DotNew.Inner dni = dn.new Inner();
    }

}

 

你可能感兴趣的:(java日常)