java程序纠错

来源:百度知道 编辑:UC知道 时间:2024/05/09 13:35:22
class Point2D{
int x;
int y;

public Point2D(int x,int y){
this.x=x;
this.y=y;
}
void offset(int a,int b){
this.x=a;
this.y=b;
System.out.println("the new point is("+this.x+","+this.y+")");
}
double Distance(int x1,int x2,int y1,int y2){
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
}
class Point3D extends Point2D{
int z;
public Point3D(int x,int y,int z){
this.x=x;
this.y=y;
this.z=z;
}
Point3D(Point2D p,int z){
this.x=p.x;
this.y=p.y;
this.z=z;
}
void offset(int a,int b,int c){
this.x=a;
this.y=b;
this.z=c;
System.out.println(this.x+","+this.y+","+this.z+",");
}
public static void main(String args[]){
Point2D p2d1=new Point2D(2,4);
Point2D p2d2=new Point2D(3,5);
System.out.println("the distance of (2,4) and (3,5) is"+p2

我帮你改了一下,构造函数和distance函数重载了

public class Point3D extends Point2D {

public static void main(String args[]) {
Point2D p2d1 = new Point2D(2, 4);
Point2D p2d2 = new Point2D(3, 5);
System.out.println("the distance of (2,4) and (3,5) is"
+ p2d1.Distance(p2d2));
}

int z;

public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}

Point3D(Point2D p, int z) {
super(p);
this.z = z;
}

void offset(int a, int b, int c) {
super.offset(a, b);
this.z = c;
System.out.println(this.z);
}
}

class Point2D {
private int x;

private int y;

public Point2D(int x, int y) {
this.x = x;
this.y = y;
}

public Point2D(Point2D p) {
this.x = p.x;
this.y = p.y;
}

double Distance(int x2, int y2) {
return Math.sqrt((th