this关键字代码演示
public class Flower {
int petalCount = 0;
String s = "initial value";
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 arg");
}
Flower() {
this("hi", 47);
System.out.println("default constructor (no args)");
}
void printPetalCount() {
System.out.println("petalCount = " + petalCount + " s = " + s);
}
public static void main(String[] args){
Flower x = new Flower();
x.printPetalCount();
}
}
代码输出
Constructor w/ int arg only,petalCount = 47
String & int arg
default constructor (no args)
petalCount = 47 s = hi