java class の annoying chatters no.3- external output, an internal variables of the class

example

Creating a class comprising inner class, defined in the outer class named string String str and initialize any value within the class is defined, the variable with the same name and type similar to the external and internal define a method in the class , respectively, and printing the outer class variables within the class.

public class Outer {	//定义一个外部类Outer
	static String str = "这是外部类的成员变量";	//定义外部类的成员变量
	class Inner {	//定义一个内部类Inner
		String str = "这是内部类的成员变量";	//定义内部类的成员变量
		public void method() {	//内部类里定义一个方法method
			syso(str);	//输出内部类的成员变量
			syso(Outer.str);	//输出外部类的成员变量
		}
	}
	public static void main(String[] args) {	//主方法
		new Outer().new Inner().method();	//调用method方法
	}
}

运行结果:
这是内部类的成员变量
这是外部类的成员变量

Sui Suinian links:
Output outer class member variables within the class, the class with the externalClass name. Variable names
Internal call a method in the class of the main process, withExternal new class () .new inner class () method name ();

Published 38 original articles · won praise 4 · Views 808

Guess you like

Origin blog.csdn.net/Hide111/article/details/105186023