-- 作者:pennyliang
-- 发布时间:12/31/2005 11:57:00 AM
-- 面向对象构造关于reference的静态构造异常[含我的回帖]
1. LiuPu 12月29日 上午10时34分 显示选项 新闻论坛:comp.object 发件人: "LiuPu" <Liup...@gmail.com> - 查找此作者的帖子 日期:28 Dec 2005 18:34:19 -0800 当地时间:2005年12月29日(星期四) 上午10时34分 主题:I can't explain this 回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 // A.java public abstract class A{ public String str = "AAA"; } // B.java public B extends A{ A a = new B(); public static void main(String[] args){ A b = new B(); } } When I use "java B" , the error "java.lang.StackOverflowError" appears. I can't explain this. 回复 2. iamfrac...@hotmail.com 12月29日 下午4时37分 显示选项 新闻论坛:comp.object 发件人: iamfrac...@hotmail.com - 查找此作者的帖子 日期:29 Dec 2005 00:37:01 -0800 当地时间:2005年12月29日(星期四) 下午4时37分 主题:Re: I can't explain this 回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 LiuPu skrev: > // A.java > public abstract class A{ > public String str = "AAA"; > } > // B.java > public B extends A{ > A a = new B(); > public static void main(String[] args){ > A b = new B(); > } > } > When I use "java B" , the error "java.lang.StackOverflowError" appears. > I can't explain this. Hej! Firstly, this has nothing to do with inheritence; you can see the problem in its simplest form in the following class: class Test { Test anotherTest = new Test(); public static void main(String[] args) { Test test = new Test(); } } The problem is cause because every Test object is creating another Test object, leading to an attempt to create an infinite amount of Test objects. So, in the main() method, the JVM pushes the execution context onto the stack when it calls the Test() constructor; but this will first attempt to initialise all field variables in Test, and the only field variable is another Test object. When this Test() constructor is called, yet another execution context must be pushed onto the stack, etc. In the end, the stack is filled and the JVM says goodbye. If you actually want your B object to hold a reference to another A object, then you'll have to declare the field variable as equal to null, and add the appropriate referenced A object after the B object is initialised (e.g., with a setter method). .ed -- www.EdmundKirwan.com - Home of The Fractal Class Composition. 回复 3. Penny Liang 12月29日 下午7时46分 显示选项 新闻论坛:comp.object 发件人: "Penny Liang" <pennyliang...@software.nju.edu.cn> - 查找此作者的帖子 日期:Thu, 29 Dec 2005 19:46:08 +0800 当地时间:2005年12月29日(星期四) 下午7时46分 主题:Re: I can't explain this 回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 for extends A, B has a static relation with A because of inheritance, for contain a reference of A,B has a dynamic relation with A, because of reference. let's see the construciton of B create A(for extends) create B(for reference) create A(for extends) create B(for reference) ........ ........ the StackOverflowError occures because of the construction function of B -- Thanks & Regards, Penny Liang Email:jackli...@vip.sina.com;pennyliang...@software.nju.edu.cn "LiuPu" <Liup...@gmail.com> ???? news:1135823659.216804.243720@o13g2000cwo.googlegroups.com... - 隐藏被引用文字 - - 显示引用的文字 - > // A.java > public abstract class A{ > public String str = "AAA"; > } > // B.java > public B extends A{ > A a = new B(); > public static void main(String[] args){ > A b = new B(); > } > } > When I use "java B" , the error "java.lang.StackOverflowError" appears. > I can't explain this. 回复 4. LiuPu 12月30日 下午1时36分 显示选项 新闻论坛:comp.object 发件人: "LiuPu" <Liup...@gmail.com> - 查找此作者的帖子 日期:29 Dec 2005 21:36:42 -0800 当地时间:2005年12月30日(星期五) 下午1时36分 主题:Re: I can't explain this 回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 Thank you for your answers. Now I know the problem and its answer.
|