Circle 实现 - 如何建模以获取此信息
1. Find circle diameter from radius
2. Find circle diameter from perimeter
3. Find circle diameter from area
4. Find circle perimeter from diameter
5. Find circle perimeter from radius
6. Find circle radius from diameter
7. Find circle radius from perimeter
8. Find circle radius from area
目前我们的模型类是这样实现的。
class Circle {
double radius;
Circle (double r) {
}
// Solves 6,7,8
double getDiameter() {}
double getPerimeter() {}
double getArea() {}
// static functions to solve 1-5
// e.g. public static double getPermiter(double diameter) {..}
}
有没有更好的方法来对上述类进行建模,以便我可以获取上述信息,因为给定某个参数(例如半径、直径、面积或周长),用户期望查找其他信息。
1. Find circle diameter from radius
2. Find circle diameter from perimeter
3. Find circle diameter from area
4. Find circle perimeter from diameter
5. Find circle perimeter from radius
6. Find circle radius from diameter
7. Find circle radius from perimeter
8. Find circle radius from area
Currently our model class is implemented like this..
class Circle {
double radius;
Circle (double r) {
}
// Solves 6,7,8
double getDiameter() {}
double getPerimeter() {}
double getArea() {}
// static functions to solve 1-5
// e.g. public static double getPermiter(double diameter) {..}
}
Is there a better way to model the above class, so that I can fetch the above information, since given a certain parameter (e.g. radius, diameter, area or perimeter) the user is expected to find other information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能更喜欢带有使用半径的构造函数的 Circle 类。您可以添加静态工厂方法来根据圆周和面积创建实例。
I'd probably prefer a Circle class with a constructor using the radius. You can add static factory methods to create instances from the circumference and area.
我会将所有 8 个方法实现为 Circle 类中的静态公共方法(或者可能在关联的辅助类中),并通过调用上面定义的正确静态方法来声明 get{Diameter,Perimeter,radius}:
I would implement all 8 methods as static public methods inside the Circle class (or maybe in an associate helper class) and also declare the get{Diameter,Perimeter,radius} by calling the proper static methods defined above:
据我了解,您并不总是需要一个圆本身,但是,例如,您想按给定的半径计算潜在的圆面积。
我宁愿将所有这些静态函数移至单独的实用程序类中。按照惯例,此类名称是与其一起使用的类或接口的复数形式。因此,它将被命名为
Circles
。如果由于某些原因您仍然需要类
Circle
,那么请考虑使您的圆不可变(如果不是),即删除任何设置器并将所有字段设置为final
。As I understand, you don't always need a circle itself, but, for instance, you want to count potential circle area by given radius.
I'd rather move all these static functions to a separate utility class. By convention, such class name is a plural form of a class or interface with which it's used. So, it will be named
Circles
.If you still need class
Circle
for some reasons then consider to make your circle immutable (if it is not) i.e. remove any setters and make all fieldsfinal
.