Think in java 答案_Chapter 4_Exercise 2
2007-01-22 14:04
459 查看
阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx
/****************** Exercise 2 ******************
* Add an overloaded constructor to Exercise 1
* that takes a String argument and prints it
* along with your message.
***********************************************/
public class E02_OverloadedConstructor {
E02_OverloadedConstructor() {
System.out.println("Default constructor");
}
E02_OverloadedConstructor(String s) {
System.out.println("String arg constructor");
System.out.println(s);
}
public static void main(String args[]) {
// Call default version:
new E02_OverloadedConstructor();
// Call overloaded version:
new E02_OverloadedConstructor("Overloaded");
}
}
//+M java E02_OverloadedConstructor
** A second way to do this is to explicitly call the default constructor inside the overloaded constructor using this:
public class E02_OverloadedConstructor2 {
E02_OverloadedConstructor2() {
System.out.println("Default constructor");
}
E02_OverloadedConstructor2(String s) {
this();
System.out.println(s);
}
public static void main(String args[]) {
// Call default version:
new E02_OverloadedConstructor2();
// Call overloaded version:
new E02_OverloadedConstructor2("Overloaded");
}
}
//+M java E02_OverloadedConstructor2
/****************** Exercise 2 ******************
* Add an overloaded constructor to Exercise 1
* that takes a String argument and prints it
* along with your message.
***********************************************/
public class E02_OverloadedConstructor {
E02_OverloadedConstructor() {
System.out.println("Default constructor");
}
E02_OverloadedConstructor(String s) {
System.out.println("String arg constructor");
System.out.println(s);
}
public static void main(String args[]) {
// Call default version:
new E02_OverloadedConstructor();
// Call overloaded version:
new E02_OverloadedConstructor("Overloaded");
}
}
//+M java E02_OverloadedConstructor
** A second way to do this is to explicitly call the default constructor inside the overloaded constructor using this:
public class E02_OverloadedConstructor2 {
E02_OverloadedConstructor2() {
System.out.println("Default constructor");
}
E02_OverloadedConstructor2(String s) {
this();
System.out.println(s);
}
public static void main(String args[]) {
// Call default version:
new E02_OverloadedConstructor2();
// Call overloaded version:
new E02_OverloadedConstructor2("Overloaded");
}
}
//+M java E02_OverloadedConstructor2
相关文章推荐
- Think in java &答案_Chapter 2_Exercise 1
- Think in java 答案_Chapter 4_Exercise 1
- Think in java 答案_Chapter 4_Exercise 18
- Think in java &答案_Chapter 2_Exercise 2
- Think in java 答案_Chapter 2_Exercise 8
- Think in java 答案_Chapter 4_Exercise 9
- Think in java 答案_Chapter 4_Exercise 19
- Think in java 答案_Chapter 5_Exercise 5
- Think in java 答案_Chapter 5_Exercise 9
- Think in java chapter's答案_Chapter 2_Exercise 3
- Think in java 答案_Chapter 3_Exercise 1
- Think in java 答案_Chapter 3_Exercise 6
- Think in java 答案_Chapter 4_Exercise 4
- Think in java 答案_Chapter 4_Exercise 10
- Think in java 答案_Chapter 4_Exercise 20
- Think in java 答案_Chapter 5_Exercise 6
- Think in java 答案_Chapter 2_Exercise 4
- Think in java 答案_Chapter 2_Exercise 9
- Think in java 答案_Chapter 3_Exercise 2
- Think in java 答案_Chapter 3_Exercise 7