用java编写程序,定义一个点类point,要求用重载方法求出两点之间的距离
1个回答
2011-10-11
展开全部
好像你的问题不够清楚,以前三个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());
}
}
/** 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());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询