package StaticInnerClassPackage; //静态内部类的初始化不需要依附外部类,在使用时才初始化。不过在初始化静态内部类的过程中也会初始化外部类。 public class OuterClass extends Person { private static int OutStaticVarPrivate = 1; static int OutStaticVarNotPrivate = 2; int OutVarNotStaticNotPrivate = 3; private int OutVarNotStaticPrivate = 4; static int SameNameVar = 15; public static void main(String[] args) { //外部类访问内部类的私有静态变量和非静态变量 System.out.println(StaticInnerClass.InnerStaticVarPrivate); System.out.println(new StaticInnerClass().InnerVarPrivate); //外部类访问内部类的非私有静态变量和非静态变量 System.out.println(StaticInnerClass.InnerStaticVar); System.out.println(new StaticInnerClass().InnerVar); new StaticInnerClass().NotStaticInnerMethod(); StaticInnerClass.StaticInnerMethod();//外部类访问内部类的静态方法需要使用 内部类名.内部类静态方法名 new StaticInnerClass().NotStaticInnerMethodPrivate(); StaticInnerClass.StaticInnerMethodPrivate(); } public void NotStaticOutMethod() { System.out.println("NotStaticOutMethod"); } public static void StaticOutMethod() { System.out.println("StaticOutMethod"); } private void NotStaticOutMethodPrivate() { System.out.println("NotStaticOutMethodPrivate"); } private static void StaticOutMethodPrivate() { System.out.println("StaticOutMethodPrivate"); } static class StaticInnerClass extends Student {//不论是私有还是非私有,规则不变,但是如果被private修饰,其他类无法创建内部类对象,就算是 new 外部类.内部类() 的格式也不行 private static int InnerStaticVarPrivate = 1; private int InnerVarPrivate = 2; static int InnerStaticVar = 3; int InnerVar = 4; static int SameNameVar = 25; public static void main(String[] args) { int SameNameVar = 35; //静态内部类中的静态方法访问外部类的静态变量和静态方法---->直接用 System.out.println(OutStaticVarNotPrivate); StaticOutMethod();//内部类访问外部类的静态方法时无需使用“外部类名.静态方法名” //无视内部类这一结构,直接按照静态访问非静态看待 System.out.println(new OuterClass().OutVarNotStaticNotPrivate); new OuterClass().NotStaticOutMethod(); //不管外部类是不是私有变量或私有方法,内部类也可以用 System.out.println(OutStaticVarPrivate); System.out.println(new OuterClass().OutVarNotStaticPrivate); StaticOutMethodPrivate(); new OuterClass().NotStaticOutMethodPrivate(); //访问局部变量、内部类变量、外部类变量(重名的) System.out.println(SameNameVar); System.out.println(StaticInnerClass.SameNameVar); System.out.println(OuterClass.SameNameVar); } public void NotStaticInnerMethod() { // System.out.println("OutVarNotStaticNotPrivate = " + OutVarNotStaticNotPrivate);//静态内部类的类体内全是静态区域,就算是其中的非静态方法也不能直接调用外部非静态变量 System.out.println("InnerVar = " + InnerVar);//凡是对于内部类本身来说,调用自己的非静态属性仍然可以算是非静态区域 System.out.println("NotStaticInnerMethod"); } public static void StaticInnerMethod() { System.out.println("StaticInnerMethod"); } private void NotStaticInnerMethodPrivate() { System.out.println("NotStaticInnerMethodPrivate"); } private static void StaticInnerMethodPrivate() { System.out.println("StaticInnerMethodPrivate"); } } class NotStaticInnerClass { public void InnerMethod() { System.out.println("OutVarNotStaticNotPrivate = " + OutVarNotStaticNotPrivate); } } }
package StaticInnerClassPackage; public class Teacher extends OuterClass{ //在其他类里面创建内部类的对象需要借助外部类 OuterClass.StaticInnerClass staticInnerClass = new OuterClass.StaticInnerClass(); }
package StaticInnerClassPackage; public class Student { public static void main(String[] args) { static int SameNameVar = 45; } }
package StaticInnerClassPackage; public class Person { static int SameNameVar = 55; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)