| 发表于:2007-01-30 11:03:28 楼主 |
public class flower { private int petalcount = 0; private string s = new string( "null "); flower(int petals) { petalcount = petals; system.out.println( "constructor w/ int arg only, petalcount= " + petalcount); } flower(string ss) { system.out.println( "constructor w/ string arg only, s= " + ss); s = ss; } flower(string s, int petals) { this(petals); //! this(s); // can 't call two! this.s = s; // another use of "this " system.out.println( "string & int args "); } flower() { this( "hi ", 47); system.out.println( "default constructor (no args) "); } void print() { //! this(11); // not inside non-constructor! system.out.println( "petalcount = " + petalcount + " s = "+ s); } public static void main(string[] args) { flower x = new flower(); x.print(); } } 我有几点不太明白.代码里的s在流程里的内容是 "hi "而一些构造函数里的this.s怎么就会是null了?希望高手能帮我分析一下this( "hi ", 47)和flower(string s, int petals)会产生什么样的影响?还有就是private string s = new string( "null ");又改变了什么?谢谢! |
|
|
|
|