我会通过在内部以单一格式(例如,正弧度)存储值来解决这个问题,并提供静态工厂方法来从各种输入格式构造角度。例如:
public class Angle {
private final double radians;
private Angle(double radians) {
this.radians = radians;
}
// Static factory methods
public static Angle radians(double rad) {
return new Angle(rad);
}
public static Angle degrees(double deg) {
return radians(Math.toRadians(deg));
}
public static Angle degreesCCW(double deg) {
return degrees(360 - deg);
}
...
// Operations
public Angle plus(Angle other) {
return new Angle((this.radians + other.radians) % (2 * Math.PI));
}
...
}
然后你可以像这样使用它:
Angle a1 = Angle.radians(0.5);
Angle a2 = Angle.degreesCCW(60);
Angle a3 = a1.plus(a2);