Given:
1. class ClassA{
2. public int numberOfInstances;
3. protected ClassA(int numberOfInstances){
4. this.numberOfInstances = numberOfInstances;
5. }
6. }
7. class ExtendedA extends ClassA{
8. private ExtendedA(int numberOfInstances){
9. super(numberOfInstances);
10. }
11. public static void main(String[] args){
12. ExtendedA ext = new ExtendedA(420);
13. System.out.print(ext.numberOfInstances);
14. }
15. }
What is the result?
A. 420 is the output
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. Constructors CANNOT use the private modifier.
E. Constructors CANNOT use the protected modifier.
-----------------------------------------------------------------------------------------------
答案:A
參考:6-5 繼承
考建構式的觀念
11. public static void main(String[] args){
12. ExtendedA ext = new ExtendedA(420);
>>
8. private ExtendedA(int numberOfInstances){ //420
9. super(numberOfInstances); //420
>>
3. protected ClassA(int numberOfInstances){ //420
4. this.numberOfInstances = numberOfInstances; //420
>>
2. public int numberOfInstances; //420
>>
13. System.out.print(ext.numberOfInstances); //420
沒有留言:
張貼留言