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 = s;
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
}麻烦问一下 程序执行的过程,他们的顺序什么?
其中几个this的用法,是什么意思!
谢谢
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 = s;
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
}麻烦问一下 程序执行的过程,他们的顺序什么?
其中几个this的用法,是什么意思!
谢谢
Flower()中调 this("hi", 47); 就相当于调用Flower("hi", 47); 这种写法的目的是让重载的构造函数可以共享数据初始化过程,尽最大过程的重用代码,也是弥补没有c++中缺省构造函数的遗憾
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表示调用Flower(String ss)的构造函数,必须是第一条语句
this.s = s; //表示,当前传进来的s,就是当前的s,不至于和域定义的s=ss,混淆,
System.out.println("String & int args");
} Flower() {
this("hi", 47); //调用Flower(String s, int petals)的构造函数
System.out.println("default constructor (no args)");
} void print() {
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
} public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
}
注意 static method中不能用this
1、Flower x = new Flower();
2、 Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
3、Flower(String s, int petals) {
this(petals);
this.s = s;
System.out.println("String & int args");
}
4、 Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only, petalCount= "
+ petalCount);
}
5、x.print();
6、 void print() {
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
this引用属性
this(petals); // this表示调用Flower(int petals)的构造函数,必须是第一条语句
this()表示相应的构造函数
this(petals);
this.s = s; //成员变量和参数名称相同,使用this加以区别
System.out.println("String & int args");
}
你看这个程序倒着往上看!!
After: staticinitialization(Flower.<clinit>) //结束Flower类的静态块初始化
Before: execution(void Flower.main(String[])) //开始执行main函数
Before: call(Flower()) //调用Flower类的构告函数
Before: preinitialization(Flower()) //
After: preinitialization(Flower())
Before: initialization(Flower()) //开始实例化
Before: execution(Flower(int)) //开始执行构造函数Flower(int petals)
Before: set(int Flower.petalCount) //初始化Flower类的成员变量petalCount的值为0
After: set(int Flower.petalCount)
Before: call(java.lang.String(String)) //构造一个String实例,即"null"
After: call(java.lang.String(String))
Before: set(String Flower.s) //将上面那个String实例,即"null"给s初始化
After: set(String Flower.s)
Before: set(int Flower.petalCount) //设置petalCount的值为47
After: set(int Flower.petalCount)
Before: get(PrintStream java.lang.System.out) //开始println
After: get(PrintStream java.lang.System.out)
Before: call(java.lang.StringBuilder(String))
After: call(java.lang.StringBuilder(String))
Before: get(int Flower.petalCount)
After: get(int Flower.petalCount)
Before: call(StringBuilder java.lang.StringBuilder.append(int))
After: call(StringBuilder java.lang.StringBuilder.append(int))
Before: call(String java.lang.StringBuilder.toString())
After: call(String java.lang.StringBuilder.toString())
Before: call(void java.io.PrintStream.println(String))
Constructor w/ int arg only, petalCount= 47
After: call(void java.io.PrintStream.println(String))
After: execution(Flower(int)) //构造函数Flower(int petals)结束
Before: execution(Flower(String, int)) //开始执行构造函数Flower(String s, int petals)
Before: set(String Flower.s) //设置s的值为"hi"
After: set(String Flower.s)
Before: get(PrintStream java.lang.System.out)//开始println
After: get(PrintStream java.lang.System.out)
Before: call(void java.io.PrintStream.println(String))
String & int args
After: call(void java.io.PrintStream.println(String))
After: execution(Flower(String, int))//构造函数Flower(String s, int petals)结束
Before: execution(Flower()) //开始执行构造函数Flower()
Before: get(PrintStream java.lang.System.out) //开始println
After: get(PrintStream java.lang.System.out)
Before: call(void java.io.PrintStream.println(String))
default constructor (no args)
After: call(void java.io.PrintStream.println(String))
After: execution(Flower()) //构造函数Flower()结束
After: initialization(Flower())
After: call(Flower())
Before: call(void Flower.print()) //下面是x.print();的执行过程
Before: execution(void Flower.print())
Before: get(PrintStream java.lang.System.out)
After: get(PrintStream java.lang.System.out)
Before: call(java.lang.StringBuilder(String))
After: call(java.lang.StringBuilder(String))
Before: get(int Flower.petalCount)
After: get(int Flower.petalCount)
Before: call(StringBuilder java.lang.StringBuilder.append(int))
After: call(StringBuilder java.lang.StringBuilder.append(int))
Before: call(StringBuilder java.lang.StringBuilder.append(String))
After: call(StringBuilder java.lang.StringBuilder.append(String))
Before: get(String Flower.s)
After: get(String Flower.s)
Before: call(StringBuilder java.lang.StringBuilder.append(String))
After: call(StringBuilder java.lang.StringBuilder.append(String))
Before: call(String java.lang.StringBuilder.toString())
After: call(String java.lang.StringBuilder.toString())
Before: call(void java.io.PrintStream.println(String))
petalCount = 47 s = hi
After: call(void java.io.PrintStream.println(String))
After: execution(void Flower.print())
After: call(void Flower.print())
After: execution(void Flower.main(String[]))
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 = s;
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print(); //应该是先调用无参数的吧。
}
}