JAVA学习之静态内部类(二)

来源:互联网 发布:hishop云商城2.0源码 编辑:程序博客网 时间:2024/05/01 15:52
/*内部类的访问格式:1.当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中直接建立内部类的对象进行访问格式:外部类名.内部类名 变量名 = 外部类对象.内部类对象Outer.Inner in = new Outer().new Inner();2.内部类在成员位置上,就可以被成员修饰符所修饰;private:将内部类封装在外部类中。static:当内部类被static修饰后,只能访问外部类中的静态成员,出现了访问局限;在外部其他类中,访问静态内部类中非静态方法的方法 new Outer.Inner().function();在外部其他类中,访问静态内部类中静态方法的方法   Outer.Inner.function();如果在内部类中定义了静态的成员,该内部类必须是静态的。如果在外部类中的静态方法要访问内部类时,该内部类也必须是静态的。*/class Outer{private static int x =3;static class Inner{static void function(){System.out.println("innner:"+x);}}public static void show(){Inner.function();            //或者new Inner().function();}}class  InnerclassDemo2{public static void main(String[] args) {//new Outer.Inner().function();Outer.Inner.function();}}

0 0