JAVA编程定义一个点类。

Python021

JAVA编程定义一个点类。,第1张

你的第二个方法要求描述不太明白,我就按照自己的理解写了一个。

我偷懒就直接在main方法里面写测试代码了,你需要的话就再自己定义一个Test类写个mian方法,内容其实没什么区别。

public class Point {

private double x

private double y

public Point(double x, double y) {

super()

this.x = x

this.y = y

}

public double distance(Point point) {

return Math.sqrt((point.x - this.x) * (point.x - this.x) + (point.y - this.y) * (point.y - this.y))

}

public Point move(double x, double y) {

return new Point(this.x + x, this.y + y)

}

@Override

public String toString() {

// TODO Auto-generated method stub

return "(" + x + ", " + y + ")"

}

public static void main(String[] args) {

Point p1 = new Point(5, 6)

Point p2 = new Point(-2, -9)

System.out.println(p1.distance(p2))

System.out.println(p1.move(11, -2))

}

}

测试结果:

16.55294535724685

(16.0, 4.0)

public class Point

{

public static void main(String[] args)

{

Point p1=new Point()

Point p2=new Point(1,2)

p1.show()

p1.move(3,4)

p1.show()

p2.show()

p2.move(5,6)

p2.show()

}

Point()

{

this(0,0)

}

Point(float x,float y)

{

this.x=x

this.y=y

}

void move(float x,float y)

{

this.x=x

this.y=y

}

void show()

{

System.out.printf("(%f,%f)",x,y)

System.out.println()

}

private float x,y

}