JAVA设计Point类,类有x,y,构造方法,求两点之间的距离,再设计一个继承Point类的Poi
JAVA设计Point类,类有x,y,构造方法,求两点之间的距离,再设计一个继承Point类的Point3D类,增加z,构建方法,求空间两点的距离的方法...
JAVA设计Point类,类有x,y,构造方法,求两点之间的距离,再设计一个继承Point类的Point3D类,增加z,构建方法,求空间两点的距离的方法
展开
1个回答
展开全部
看看懂再问我哈
import java.awt.Color;
public class Point {
private int x;//x坐标
private int y;//y坐标
private Color color;//颜色
public Point() {//带参数构造
}
public Point(int x,int y) {//带两参数构造
this.x = x;
this.y = y;
}
public Point(int x,int y,Color color) {//带三参数构造
this.x = x;
this.y = y;
this.color = color;
}
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;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int distance(Point b) {//求两点距离
return (int)Math.sqrt((this.x-b.getX())*(this.x-b.getX())+(this.y-b.getY())*(this.y-b.getY()));
}
public boolean equals(Point b) {//重写equals
double c = (this.x-b.getX())*(this.x-b.getX())+(this.y-b.getY())*(this.y-b.getY());
if(c==0) {
return true;
}else return false;
}
public static void main(String[]args){//测试main
Point A=new Point();
Point B=new Point(50,60);
B.setColor(Color.red);
Point C=new Point(100,200,Color.red);
System.out.println("B: ("+B.getX()+","+B.getY()+") "+"color:"+B.getColor());
A.setX(100);
A.setY(200);
A.setColor(Color.red);
System.out.println("A==B? "+A.equals(B));
System.out.println("A->B "+A.distance(B));
}
}
import java.awt.Color;
public class Point {
private int x;//x坐标
private int y;//y坐标
private Color color;//颜色
public Point() {//带参数构造
}
public Point(int x,int y) {//带两参数构造
this.x = x;
this.y = y;
}
public Point(int x,int y,Color color) {//带三参数构造
this.x = x;
this.y = y;
this.color = color;
}
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;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int distance(Point b) {//求两点距离
return (int)Math.sqrt((this.x-b.getX())*(this.x-b.getX())+(this.y-b.getY())*(this.y-b.getY()));
}
public boolean equals(Point b) {//重写equals
double c = (this.x-b.getX())*(this.x-b.getX())+(this.y-b.getY())*(this.y-b.getY());
if(c==0) {
return true;
}else return false;
}
public static void main(String[]args){//测试main
Point A=new Point();
Point B=new Point(50,60);
B.setColor(Color.red);
Point C=new Point(100,200,Color.red);
System.out.println("B: ("+B.getX()+","+B.getY()+") "+"color:"+B.getColor());
A.setX(100);
A.setY(200);
A.setColor(Color.red);
System.out.println("A==B? "+A.equals(B));
System.out.println("A->B "+A.distance(B));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询