哈哈,我来啦!如果有看不懂的给我留言啊!(draw的要求不明白,所以draw方法里面的代码没写,你把要求重新描述一下,我帮你重写一下)
Point2D的代码:
package com.m;
public class Point2D
{
private int x;
private int y;
public Point2D(int x, int y)
{
this.x=x;
this.y=y;
}
public void offset(int a, int b)
{
x=x+a;
y=y+b;
}
public void draw()
{
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Point3D的代码:
package com.n;
import com.m.Point2D;
public class Point3D extends Point2D
{
private int z;
public Point3D(int x, int y, int z)
{
super(x,y);
this.z=z;
}
public Point3D(Point2D p, int z)
{
super(p.getX(), p.getY());
this.z=z;
}
public void offset(int a, int b,int c)
{
super.offset(a, b);
z=z+c;
}
public void draw()
{
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
public String toString()
{
return "Point3D:[x :"+getX()+" y :"+getY()+" z :"+getZ()+"]";
}
}
TestPoint代码:
import com.m.Point2D;
import com.n.Point3D;
public class TestPoint {
public static void main(String[] args)
{
double result;
int x,y,z;
Point2D p2d1,p2d2;
p2d1=new Point2D(1,2);
p2d2=new Point2D(3,4);
x=p2d1.getX()-p2d2.getX();
y=p2d1.getY()-p2d2.getY();
result=Math.sqrt(x*x+y*y);
System.out.println("p2d1与p2d2之间的距离为"+result);
Point3D p3d1,p3d2;
p3d1=new Point3D(1,2,3);
p3d2=new Point3D(4,5,6);
x=p3d1.getX()-p3d2.getX();
y=p3d1.getY()-p3d2.getY();
z=p3d1.getZ()-p3d2.getZ();
result=Math.sqrt(x*x+y*y+z*z);
System.out.println("p3d1与p3d2之间的距离为"+result);
}
}
温馨提示:内容为网友见解,仅供参考