자바 구조 시 멤버 초기 화 함정

1354 단어 자바htmlBlogOO
http://www.ehelper.com.cn/blog/post/40.html

public class A
{
	public A()
	{
		System.out.println("This is A()");
		this.Say();
	}

	public void Say()
	{
		System.out.println("This is A.Say()");
	}
}

public class B extends A
{
	public String name = "111";

	public B()
	{
		System.out.println("This is B()\t" + "name is " + name);
		name = "333";
	}

	public void Say() //   
	{
		name = "222";
		System.out.println("This is B.Say()\t" + "name is " + name);
	}

	public static void main(String[] args)
	{
		B oo = new B();
		System.out.println("This is main()\t" + "name is " + oo.name);
	}
}

This is A()
This is B.Say()	name is 222
This is B()	name is 111
This is main()	name is 333

 、 A     ;
 、 A    Say()  B ,   B    A Say(), A   Say()   ;
 、 B     ,       name = "111"。


좋은 웹페이지 즐겨찾기