Java入门:定义MyPoint类

来源:百度知道 编辑:UC知道 时间:2024/05/02 13:51:39
定义MyPoint类(表示点),重载toString()方法。在TestMyPoint.java的main方法中实例化MyPoint类的对象start和end,并通过字符串连接运算输出如下格式:
start point is start[x,y]
end point is end[x,y]

MyPoint.java

public class MyPoint{
private double x;
private double y;

public MyPoint(){
this.x=0;
this.y=0;
}

public MyPoint(double x, double y){
this.x=x;
this.y=y;
}

public double getX(){
return this.x;
}

public double getY(){
return this.y;
}

public void setX(double x){
this.x=x;
}

public void setY(double y){
this.y=y;
}

public String toString(){
return "["+this.x+","+this.y+"]";
}
}

TestMyPoint.java
public class TestMyPoint{
public static void main(String[] args){
Point start=new Point();
Point end=new Point(2,3);
System.out.println("start point is start"+s