Circle 实现 - 如何建模以获取此信息

发布于 2024-10-09 22:42:35 字数 706 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹏雨一样淡蓝的深情 2024-10-16 22:42:35

我可能更喜欢带有使用半径的构造函数的 Circle 类。您可以添加静态工厂方法来根据圆周和面积创建实例。

public class Circle
{
    double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public static Circle fromCircumference(double circumference)
    {
        return new Circle(circumference / (Math.PI * 2));
    }

    public static Circle fromArea(double area)
    {
        return new Circle(Math.sqrt(area / Math.PI));
    }

    public double getCircumference()
    {
        return 2 * Math.PI * radius;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getDiameter()
    {
        return 2 * radius;
    }

    public double getArea()
    {
        return Math.PI * radius * radius;
    }
}

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.

public class Circle
{
    double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public static Circle fromCircumference(double circumference)
    {
        return new Circle(circumference / (Math.PI * 2));
    }

    public static Circle fromArea(double area)
    {
        return new Circle(Math.sqrt(area / Math.PI));
    }

    public double getCircumference()
    {
        return 2 * Math.PI * radius;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getDiameter()
    {
        return 2 * radius;
    }

    public double getArea()
    {
        return Math.PI * radius * radius;
    }
}
咋地 2024-10-16 22:42:35

我会将所有 8 个方法实现为 Circle 类中的静态公共方法(或者可能在关联的辅助类中),并通过调用上面定义的正确静态方法来声明 get{Diameter,Perimeter,radius}:

public class Circle {
  double radius;

  public double getPerimeter() {
    return getPerimeterFromRadius(radius);
  }

  // ... 

  public static double getPerimeterFromRadius(double radius) {
    return 2 * Math.PI * 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:

public class Circle {
  double radius;

  public double getPerimeter() {
    return getPerimeterFromRadius(radius);
  }

  // ... 

  public static double getPerimeterFromRadius(double radius) {
    return 2 * Math.PI * radius;
  }

}
思慕 2024-10-16 22:42:35

据我了解,您并不总是需要一个圆本身,但是,例如,您想按给定的半径计算潜在的圆面积。

我宁愿将所有这些静态函数移至单独的实用程序类中。按照惯例,此类名称是与其一起使用的类或接口的复数形式。因此,它将被命名为Circles

public class Circles {

    public static double diameterFromRadius(double radius) {...}

    public static double diameterFromPerimeter(double perimeter) {...}

    public static double perimeterFromArea(double area) {...}

    ...
}

如果由于某些原因您仍然需要类Circle,那么请考虑使您的圆不可变(如果不是),即删除任何设置器并将所有字段设置为final

public class Circle {
  final double radius;

  public Circle (double r) {
      this.radius = r;
  }

  public double getRadius() {
      return radius;
  }
}

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.

public class Circles {

    public static double diameterFromRadius(double radius) {...}

    public static double diameterFromPerimeter(double perimeter) {...}

    public static double perimeterFromArea(double area) {...}

    ...
}

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 fields final.

public class Circle {
  final double radius;

  public Circle (double r) {
      this.radius = r;
  }

  public double getRadius() {
      return radius;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文