问答题 【说明】一条直线是由两个点组成的,代码如下。 public class Point { private int x, y; //coordinate public Point (int x, int y) {{{U}} (1) {{/U}}=x;{{U}} (2) {{/U}};} public int GetX() { return x; } public int GetY() { return y; } } class Line //line segment { private{{U}} (3) {{/U}}; //extremc points Line (Point a, Point b) //constructor { p1 ={{U}} (4) {{/U}}; p2={{U}} (5) {{/U}}; } public double Length() { return Math.sqrt (Math.pow (p2.GetX()-pl.GetX(),2) +Math.pow (p2.GetY()-p1.GetY(),2)) ; } }
【正确答案】
【答案解析】[解析] (1)this.x 构造函数Point将形参x和y赋值给当前对象成员x和y,因此必须用this指针区别。 (2)this.y=y 构造函数Point将形参x和y赋值给当前对象成员x和y,因此必须用this指针区别。 (3)Point p1,p2 Point类的p1,p2两个点对象指针,组合成Line对象。 (4)new Point(a.GetX(),a.GetY()) 给Line对象的两个点对象指针各自生成对象Point,并用a和b点对象为其赋初值。 (5)new Point (b.GetX(),b,GetY()) 给Line对象的两个点对象指针各自生成对象Point,并用a和b点对象为其赋初值。