英文:
How to make an object out of another object
问题
基本上我正在尝试使endPosition
获取position
的值并添加那些x,y
值。
我只是在努力找出正确的语法来实现这样做。
Point position = new Point((int) (Math.random() * (max - min)), (int) (Math.random() * (max - min)));
Point endPosition = new Point();
Point endPosition = (position.x + 2);
英文:
Basically I am trying to make an endPosition
take the values from position and add to those x, y
values.
I am just struggling to figure out the right syntax to do so.
Point position= new Point((int) (Math.random()*(max - min)), (int) (Math.random() *(max - min)));
Point endPosition = new Point();
Point endPosition = (position.x + 2);
答案1
得分: 3
只需在构造函数中创建一个新对象,并传入所需的参数。
Point position = new Point((int) (Math.random() * (max - min)), (int) (Math.random() * (max - min)));
Point endPosition = new Point(position.x + 2, position.y + 3);
英文:
Simply create a new object and pass the required arguments in the constructor.
Point position= new Point((int) (Math.random()*(max - min)),(int) (Math.random() *(max - min)));
Point endPosition = new Point(position.x+2, position.y+3);
答案2
得分: 0
所以你想要添加(int) (Math.random()*(max - min)
和 (int) (Math.random() *(max - min)
。
你可以在类中创建两个数据成员,称为 int x
和 int y
,以及第三个数据成员,称为 int sum
,
然后进行以下操作:
class Point {
int x;
int y;
int sum;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
然后在主函数中,简单地创建该类的一个实例。
Point position = new Point((int) (Math.random()*(max - min)), (int) (Math.random() *(max - min)));
position.sum = position.x + position.y;
英文:
So you want to add (int) (Math.random()*(max - min)
and (int) (Math.random() *(max - min)
You can create 2 data member in the class say int x
and int y
and a third data member say int sum
and do the following
class Point{
int x;
int y;
int sum;
Point(int x,int y){
this.x = x;
this.y = y;
}
}
And then in main Simply create an instance of the class.
Point position = new Point((int) (Math.random()*(max - min)),(int) (Math.random() *(max - min)));
position.sum = position.x+position.y;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论