java 编程创建一个Point类
java编程创建一个Point类,在其中定义两个变量表示一个点的坐标值,再定义构造函数初始化为坐标原点,然后定义一个方法实现点的移动,再定义一个方法打印当前点的坐标。并创...
java 编程创建一个Point类,在其中定义两个变量表示一个点的坐标值,再定义构造函数初始化为坐标原点,然后定义一个方法实现点的移动,再定义一个方法打印当前点的坐标。并创建一个对象验证。
展开
4个回答
展开全部
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;
}
/**
* 移动到点(dest1,dest2)
* @param dest1
* @param dest2
*/
public void moveTo(float dest1, float dest2) {
this.x = dest1;
this.y = dest2;
}
public void printPoint() {
System.out.print("当前点坐标:" + "(" + x + "," + y + ")\n");
}
}
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;
}
/**
* 移动到点(dest1,dest2)
* @param dest1
* @param dest2
*/
public void moveTo(float dest1, float dest2) {
this.x = dest1;
this.y = dest2;
}
public void printPoint() {
System.out.print("当前点坐标:" + "(" + x + "," + y + ")\n");
}
}
展开全部
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)
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)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个,其实没什么技术含量的
新建一个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());
}
}
新建一个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());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询