public class Vector {
private final double deltaX,deltaY;
public Vector(double deltaX, double deltaY) {
this.deltaX = deltaX;
this.deltaY = deltaY;
public Vector plus(Vector(a, b)){
return new Vector(this.deltaX+a,this.deltaY+b);
}
当我尝试创建一种方法来将新 vector 添加到现有 vector 时,为什么这不起作用? 我将 deltaX 定义为水平分量,将 deltaY 定义为垂直分量。
最佳答案
您没有使用正确的语法。你的方法应该是:
public Vector plus(Vector other) {
return new Vector(this.deltaX + other.deltaX, this.deltaY + other.deltaY);
}
这样,有人就可以将 Vector 实例传递到该方法中。
关于java - 学习有关对象的基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824640/