Imagine having this function in JavaScript:
this.add = function (x, y) {
if (x instanceof Vector2d) {
this.x += x.x;
this.y += x.y;
return this;
}
this.x += x;
this.y += y;
return this;
};
Should I overload it in Java like this:
public Vector2D add(Vector2D other) {
this.x += other.x;
this.y += other.y;
return new Vector2D(this.x, this.y);
}
public Vector2D add(double number) {
this.x += number;
this.y += number;
return new Vector2D(this.x, this.y);
}
Is there a more efficient way to handle these operations?