static修饰内部类

创建内容类的方式通过外部类的实例对象来创建

public class AA {
     int a =1;
     class BB {
          int b=3 ;
     }
     public static void main(String[] args) {
          AA a =new AA();
          BB b=a.new BB();
          System.out.println(a.a);
          System.out.println(b.b);
     }
}


利用static修饰后,可以直接创建

public class AA {
     int a =1;
     static class BB {
          int b=3 ;
     }
     public static void main(String[] args) {
          AA a =new AA();
          BB b=new AA.BB();
          System.out.println(a.a);
          System.out.println(b.b);
     }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章