Think in java 答案_Chapter 4_Exercise 4
2007-01-22 14:09
525 查看
阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx
/****************** Exercise 4 ******************
* Complete Exercise 3 by creating objects to
* attach to the array of references.
***********************************************/
public class E04_ObjectArray {
E02_OverloadedConstructor[] array =
new E02_OverloadedConstructor[5];
public E04_ObjectArray() {
for(int i = 0; i < array.length; i++)
array[i] = new E02_OverloadedConstructor();
}
// An overloaded constructor that calls the
// overloaded constructor from Exercise 2:
public E04_ObjectArray(String s) {
for(int i = 0; i < array.length; i++)
array[i] = new E02_OverloadedConstructor(s);
}
public static void main(String args[]) {
new E04_ObjectArray();
new E04_ObjectArray("Overloaded");
}
}
//+M java E04_ObjectArray
**Since the array is defined as a field of the class, it must be initialized in the constructor (if you try to initialize it inside main( ) you’ll get a complaint that a non-static field cannot be accessed inside a static method). I’ve gone the extra step here and created a second, overloaded constructor that initializes the array by calling the overloaded constructor from Exercise 2. The output is:
Default constructor
Default constructor
Default constructor
Default constructor
Default constructor
Default constructor
Overloaded
Default constructor
Overloaded
Default constructor
Overloaded
Default constructor
Overloaded
Default constructor
Overloaded
/****************** Exercise 4 ******************
* Complete Exercise 3 by creating objects to
* attach to the array of references.
***********************************************/
public class E04_ObjectArray {
E02_OverloadedConstructor[] array =
new E02_OverloadedConstructor[5];
public E04_ObjectArray() {
for(int i = 0; i < array.length; i++)
array[i] = new E02_OverloadedConstructor();
}
// An overloaded constructor that calls the
// overloaded constructor from Exercise 2:
public E04_ObjectArray(String s) {
for(int i = 0; i < array.length; i++)
array[i] = new E02_OverloadedConstructor(s);
}
public static void main(String args[]) {
new E04_ObjectArray();
new E04_ObjectArray("Overloaded");
}
}
//+M java E04_ObjectArray
**Since the array is defined as a field of the class, it must be initialized in the constructor (if you try to initialize it inside main( ) you’ll get a complaint that a non-static field cannot be accessed inside a static method). I’ve gone the extra step here and created a second, overloaded constructor that initializes the array by calling the overloaded constructor from Exercise 2. The output is:
Default constructor
Default constructor
Default constructor
Default constructor
Default constructor
Default constructor
Overloaded
Default constructor
Overloaded
Default constructor
Overloaded
Default constructor
Overloaded
Default constructor
Overloaded
相关文章推荐
- Think in java &amp;amp;答案_Chapter 2_Exercise 1
- Think in java 答案_Chapter 4_Exercise 1
- Think in java 答案_Chapter 4_Exercise 18
- Think in java &amp;答案_Chapter 2_Exercise 2
- Think in java 答案_Chapter 2_Exercise 8
- Think in java 答案_Chapter 4_Exercise 2
- 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 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