我開始研究 Java 中的 OOP 概念,并嘗試為點、線、平面和曲面創建一個基本類,但我創建的方法遇到了問題。public class Punto{ public double x, y; public Punto(double pX, double pY){ this.x = pX; this.y = pY; } public String puntoTela(){ return "( " + x + " , " + y + " )"; } public void rotacion(double grado) { double rad = (grado * Math.PI) / 180; this.x = (this.x * Math.cos(rad) + this.y * - Math.sin(rad)); this.y = (this.x * Math.sin(rad) + this.y * Math.cos(rad)); } }我的問題是:如果我聲明Punto p = new Punto(5, 2)并調用該rotation方法,我將無法回到原始值。public class Ejemplo { public static void main(String[] args){ Punto p = new Punto(5, 2); System.out.println(p.puntoTela()); // shows (5, 2) p.rotacion(45); System.out.println(p.puntoTela()); // shows (5, 2) after rotatin 90 deg = (2.1, 2.9) System.out.println(p.x); // 2.1 i want original value of 5 }}我試圖為旋轉方法創建局部變量,但它不起作用我該怎么辦??謝謝!
2 回答
搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
你基本上有兩個選擇:
創建一個
void rotate(double grado)改變對象本身的方法創建一個方法
Punto rotated(double grado),該方法返回一個Point相當于原點旋轉一定量的新點
你不能做的是讓一個對象同時旋轉而不是旋轉。
添加回答
舉報
0/150
提交
取消
