Topic: 构造函数内部调用,输出来的顺序恰好相反。 |
Print this page |
1.构造函数内部调用,输出来的顺序恰好相反。 | Copy to clipboard |
Posted by: laies Posted on: 2006-03-15 11:45
输出的是: "Constructor w/ int arg only, petalCount= 47", "String & int args", "default constructor (no args)", "petalCount = 47 s = hi" 而不是: "default constructor (no args)", "String & int args", "Constructor w/ int arg only, petalCount= 47", "petalCount = 47 s = hi" >this("hi", 47); System.out.println("default constructor (no args)"); 的确在调用另一个构造函数,但也应该随后“System.out.println("default constructor (no args)");”呀。 |
2.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: Skybus Posted on: 2006-03-15 12:15 I think the output is just right! When you initialize a new instance with "Flower x = new Flower();", the constructor "Flower()" will be called,but in that constrctor the expression "this("hi", 47)" will call the constructor "Flower(String s, int petals)";at this time ,the control has benn transfered to the constructor "Flower(String s, int petals)",so it will not print "default constructor (no args)"immediately. |
3.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: ranchgirl Posted on: 2006-03-15 12:46 Q. What is the calling order of constructors along the hierarchy, from child up, or from ancestor down? A: The order is from child up. However, the super() is always implicitly or explicitly called as the first statement before any code in the child being executed. This might cause some confusion here. If you put a print statement in each of the constructor, the ancestor one will be output first. The following code will illustrate how it works.This is called LIFO, which one is called the first, will be finished the last. The child is called first, but it call its parent as the first statement, and the immediate parent will call its parent as the first statement too, and so on. Therefore the far ancestor, which is the Object will finish its constructor the first, then its immediate child, and so on. That is why the print statements will finish from the ancestor down. The calling stack is working this way. Last In, First Out (LIFO)
copied from http://bobcat.webappcabaret.net/javachina/faq/03.htm#inh_Q130 |
4.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: gggcel_5200 Posted on: 2006-03-15 15:26 迷茫,。。。。完全的看不明白。我是新手。呵呵。 |
5.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: laies Posted on: 2006-03-15 21:55 感谢Skybus 的精彩解答,我的英语水平差,译了我半天. 感谢ranchgirl 所提供参考资料. 我明白了构造函数的含义,顺序是从孩子开始的,但孩子涉及父亲的血缘,所以先要找到父亲,而父亲又涉及父亲的父亲的血缘,而需找到孩子的爷爷. > Flower x = new Flower(); 调用Flower构造函数,让我们找到了Flower() ,而 this("hi", 47);使用this又指向了当前这个参数,而跟他匹配的是Flower(String s, int petals),随后petals也被赋予了值,通过this(petals);this又指向了当前这个参数,与他相匹配的当然是Flower(int petals)了,整个构造函数调用完毕. >x.print(); 这里调用的是方法print(),而变量petalCount 和s也是构造函数所给的值. 这里面没有引用Flower(String ss)构造函数,没有与它相匹配的,也是多余的. 非常羡慕Skybus 和 ranchgirl 的英语水平,看到打出的大堆英文,感到自己很羞愧. 我要努力的学好英语,感谢大家解答问题,并给我了动力. |
6.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: laies Posted on: 2006-03-15 22:55 ranchgirl 兄,能告诉我,这个javachina网是如何使用的. 主要围绕的是什么? 我摸索了半天没有头绪.望..... http://bobcat.webappcabaret.net/javachina/faq/03.htm#inh_Q130 |
7.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: jackyangf Posted on: 2006-03-17 14:33 很正常,一个函数要完成了才会执行到下一语句 begin with : step 1 -> // Flower() called new Flower() : { this("hi", 47); step 2 -> Flower(String s, int i) called { this(petals); step 3 -> Flower(int i) called { petalCount = petals; print first --> System.out.println("Constructor w/ int arg only, petalCount= "+ petalCount); } this.s = s; print second -->System.out.println("String & int args"); } print third --> System.out.println("default constructor (no args)"); } step 4 ->x.print(); { print last --> System.out.println( "petalCount = " + petalCount + " s = "+ s); } |
8.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: jackyangf Posted on: 2006-03-17 14:34 调构造函数也一样,不可能说第一个函数的其他语句敲定之后再执行前面的函数调用吧 |
9.Re:构造函数内部调用,输出来的顺序恰好相反。 [Re: laies] | Copy to clipboard |
Posted by: jackyangf Posted on: 2006-03-17 14:39 你这里这个 Flower 类不牵扯到继承之类的,只是本身的构造函数调用了其他函数而已 但相似的事情也能发生在继承上: 所有子类的构造函数第一句都是调父类的构造函数,所以会发生先完成基类的输出(如果有的话)然后一层层下来知道完成子类的构造函数 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |