# java 语言问题 编程语法

import java.util.Scanner;
import java.lang.Math;
class Point2D
{
int x;
int y;
public Point2D(int i,int j)
{
x=i;
y=j;
}
public void offset(int a,int b)
{
x=x+a;
y=y+b;
}
}
class Point3D extends Point2D
{
int x;
int y;
int Z;
public Point3D(int x, int y,int z)
{
super(x,y);
Z=z;
}
public Point3D(Point2D p,int z)
{
super(p.x,p.y);
Z=z;
}
public void offset(int a,int b,int c)
{
x+=a;
y+=b;
Z+=c;
}
public static void main(String args[])
{
int x=0,y=0,x1=0,y1=0,a=0,b=0,c=0,a1 = 0,b1=0,c1=0;
double sum1=0;
double sum2=0;
System.out.println("输入整数坐标x和y");
Point2D p2d1=new Point2D(x,y);
//Point2D p2d1=new Point2D(1,1);
System.out.println("输入整数坐标x1和y1");
Point2D p2d2=new Point2D(x1,y1);
//Point2D p2d2=new Point2D(2,2);
sum1=Math.sqrt(Math.pow((p2d1.x-p2d2.x),2)+Math.pow((p2d1.y-p2d2.y),2));
System.out.println("("+x+","+y+")"+"与"+"("+x1+","+y1+")"+"的距离为："+sum1);
System.out.println("输入整数坐标a,b和c");
Point3D p3d1=new Point3D(a,b,c);
System.out.println("输入整数坐标a1,b1和c1");
Point3D p3d2=new Point3D(a1,b1,c1);
sum2=Math.sqrt(Math.pow((p3d1.x-p3d2.x),2)+Math.pow((p3d1.y-p3d2.y),2)+Math.pow((p3d1.Z-p3d2.Z),2));
System.out.println("("+a+","+b+","+c+")"+"与"+"("+a1+","+b1+","+c1+")"+"的距离为："+sum2);
}
}

0

Point3D 的x/y属性没有初始化，去掉 Point3D 的x/y属性，直接用 Point2D 的x/y属性。复写下offset方法

``````public void offset(int a,int b,int c)
{
super.offset(a, b);
Z+=c;
}``````