Discuss / Java / this的指向

this的指向

Topic source

this的指向Demo:

class A{
	private String name;
	A(String name){
		this.name = name;
	}
	class B{
		private int age;
		B(int age){
			this.age = age;
		}
		String getName() {
			return A.this.name + ", age is " + this.age;			
		}
	}
}

public class Hello {	
	public static void main(String[] args) {
		A a1 = new A("zhangsan");
		A a2 = new A("lisi");
		A.B b1 = a1.new B(18);
		A.B b2 = a2.new B(22);
		System.out.println(b1.getName());
		System.out.println(b2.getName());
	}

}

可以多参考下~


  • 1

Reply