永发信息网

java 编程创建一个Point类

答案:4  悬赏:40  手机版
解决时间 2021-11-19 03:37
  • 提问者网友:活着好累
  • 2021-11-18 15:57
java 编程创建一个Point类
最佳答案
  • 五星知识达人网友:未来江山和你
  • 2021-11-18 17:01
public class Test102 {

public static void main(String[] args) {
Point point = new Point();
point.printPoint();
point.moveTo(1, 3);
System.out.println("--移动后--");
point.printPoint();
}

}

class Point {
private float x;
private float y;

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

public void moveTo(float dest1, float dest2) {
this.x = dest1;
this.y = dest2;
}

public void printPoint() {
System.out.print("当前点坐标:" + "(" + x + "," + y + ")\n");
}
}
全部回答
  • 1楼网友:山有枢
  • 2021-11-18 20:25
这个,其实没什么技术含量的
新建一个Point.java文件
public class Point{
private int x;
private int y;
//构造函数(无参),创建对象时被调用
public Point(){
x = 0;
y = 0;
}
//构造函数(有参),创建对象时被调用
public Point(int x,int y){
this.x = x;
this.y = y;
}

public int getX(){
return this.x;
}
public void setX(int newX){
this.x = newX;
}
//设置y的自己加,懒得写
}
然后在另一个文件TestPoint.java中
public class TestPoint{
public static void main(String[] args){
Point p1 = new Point();
System.out.println("x="+p1.getX()+",y="+p1.getY());
Point p2 = new Point(2,2);
System.out.println("x="+p2.getX()+",y="+p2.getY());

}
}
  • 2楼网友:末日狂欢
  • 2021-11-18 19:57
好复杂呀!
  • 3楼网友:十鸦
  • 2021-11-18 18:30
public class Point {

private double x;
private double y;

//默认构造坐标原点
public Point(){
this.x = 0d;
this.y = 0d;
}

//测试
public static void main(String[] args) {
Point p = new Point();
p.printCurrentPosition();//打印当前位置坐标原点
p.move(22, 33.5);//移动
p.printCurrentPosition();

}

public void move(double newX, double newY){
this.x = newX;
this.y = newY;
}

public void printCurrentPosition(){
System.out.println("Position now is: (" + x + ", " + y + ")");
}

}
-----------testing
Position now is: (0.0, 0.0)
Position now is: (22.0, 33.5)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯