作者idman (9527)
站内java
标题[问题] 物件的问题
时间Fri Jan 12 09:55:41 2007
//修改物件的属性
class MyPoint
{
protected double x;
protected double y;
MyPoint()
{
setX(0);
setY(0);
}
MyPoint(double x, double y)
{
setX(x);
setY(y);
}
public void setX(double x)
{
this.x = x;
}
public void setY(double y)
{
this.y = y;
}
public double x()
{
return x;
}
public double y()
{
return y;
}
public String toString()
{
return "[" + x + "," + y + "]";
}
}
class MyLine
{
private MyPoint pt1, pt2;
MyLine(MyPoint pt1, MyPoint pt2)
{
this.pt1 = pt1;//对物件设值
this.pt2 = pt2;//对物件设值
}
public double getMyLineLength()
{
return Math.sqrt((pt2.x() - pt1.x())
*
(pt2.x() - pt1.x())
+
(pt2.y() - pt1.y())
*
(pt2.y() - pt1.y()));
}
public String toString()
{
return "point1:" + pt1 + ",point2:" + pt2;
}
}
public class Demo108
{
public static void main(String[] args)
{
MyPoint pt1 = new MyPoint(1, 1);
MyPoint pt2 = new MyPoint(2, 2);
MyLine line1 = new MyLine(pt1, pt2);
System.out.println("line1 = " + line1);
//修改物件的属性,使物件的内部状态发生变化
//就是对物件设值
pt1.setX(0);
pt1.setY(0);
System.out.println("修改了线段起点以後");
System.out.println("line1 = " + line1);
}
}
我的问题是 当中并没有呼叫 toString()这个函数
请问为什麽会执行到当中的值呢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.57.132.122