程序5-5
public class Point {
// 其他构造方法
public double distance() {
return Math.sqrt( x*x + y*y );
}
// 其他的成员方法
static void main(String args[]) {
Point p = new Point(1,1);
System.out.println("p.distance() = " + p.distance());
p = new Point3d(1,1,1);
System.out.println("p.distance() = " + p.distance());
}
}
class Point3d extends Point{
public double distance(){
return Math.sqrt( x*x + y*y + z*z );
}
}