真题分析
面向对象程序设计
内部类
(1)Given the following Java code:
10.class Line {
11.public static class Point {}
12.}
13.
14.class Triangle {
15.//insert code here
16.}
Which code, inserted s line 15, creates an instance of the Point class defined in Line ?
A. Point p = new Point();
B. Line.Point p = new Line.Point();
C. The Point class cannot be instantiated a t line 15.
D. Lind 1 = new Line(); 1.Point p = new 1.Point();
答案:B
解析:
� 在第 15 列所加入的程序代码, 要能够建构出 Point 类别之实体.
� Point 是 Line 中的内部 static 类别
� 对于 Tringle 而言, 必先有 Line 实体的存在, 才会有 Point 实体存在, 因此, 先建
构出 Line.
� 接下来有了 Line 实体, 再以 static 成员的对待方式处理之, 因此以外部类别实体
直接呼叫其建构方法即可, 不用再 new, 因为是 static 成员.
� 宣告型态则为 Line.Point
� Line.Poinr p = new Line.Point()
(2)Given the following Java code:
package geometry;
public class Hypotenuse {
public InnerTriangle it = new InnerTriangle();