java中this关键字用法
2009-11-12 20:59:36 来源:WEB开发网核心提示:view plaincopy to clipboardPRint?/** * this关键字用法 */public class Flower { int petalCount = 0; String s = "initial value"; Flower(int petals){ petalCo
view plaincopy to clipboardPRint?
/**
* this关键字用法
*/
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals){
petalCount = petals;
System.out.println("Constructor with one int arg");
}
Flower(String ss){
System.out.println("Constructor with one string arg");
s = ss;
}
Flower(String ss, int petals){
this(petals);
this.s = ss;
System.out.println("Constructor with string and int args");
}
Flower(){
this("hi",47);
System.out.println("Default Constructor with no args");
}
void printPetalCount(){
System.out.println("petalCount = "+petalCount+"; s = "+s);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Flower x = new Flower();
x.printPetalCount();
}
}
/**
* this关键字用法
*/
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals){
petalCount = petals;
System.out.println("Constructor with one int arg");
}
Flower(String ss){
System.out.println("Constructor with one string arg");
s = ss;
}
Flower(String ss, int petals){
this(petals);
this.s = ss;
System.out.println("Constructor with string and int args");
}
Flower(){
this("hi",47);
System.out.println("Default Constructor with no args");
}
void printPetalCount(){
System.out.println("petalCount = "+petalCount+"; s = "+s);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Flower x = new Flower();
x.printPetalCount();
}
}
更多精彩
赞助商链接