62,623
社区成员
发帖
与我相关
我的任务
分享class Gouzao {
Gouzao() {
this(6); //this语句必须放在构造方法第一行
System.out.println("Gouzao()");
}
Gouzao(int i) {
System.out.println("Gouzao(int i)");
}
void f() {
System.out.println("123");
}
}
public class P59 {
public static void main(String[] args) {
// TODO 自动生成方法存根
new Gouzao();
}
}//: Flower.java
// Calling constructors with "this"
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();
}
} ///