用java编写程序,定义一个点类point,要求用重载方法求出两点之间的距离

如题所述

好像你的问题不够清楚,以前三个Java类,其中一个测试类,toString方法体现了多态.

/** Point.java */
public class Point {
public double x;
public double y;

public Point(double x,double y){
this.x = x;
this.y = y;
}

@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}

}
/**ColorPoint.java*/
import java.awt.Color;

public class ColorPoint extends Point{
public Color color;
public ColorPoint(double x, double y,Color color) {
super(x, y);
this.color = color;
}
@Override
public String toString() {
return "ColorPoint [color=" + color + ", x=" + x + ", y=" + y + "]";
}

}
/**TestPoint.java*/
import java.awt.Color;

public class TestPoint {

public static void main(String[] args) {
Point point = new Point(3,4);
ColorPoint colorPoint = new ColorPoint(2,3,Color.black);
System.out.println(point.toString());
System.out.println(colorPoint.toString());
}

}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答