重载计算各种形状面积的静态方法,重复方法警告

Overloaded static methods that calculate the area of various shapes, duplicate method warning

我正在阅读从Java开始,书中提出的挑战是:"Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: circles, rectangles and cylinders."圆的面积只需要一个参数,这里没有问题,所以我忽略了那个方法。但是矩形的面积和圆柱的面积都需要两个参数:

public class Area{
    public static double area(double w, double l)
    {
      //Area of rectangle
        return l*w;
    }
    public static double area(double r, double h)
    {
      //Area of a cylinder
        return Math.PI * (r*r) *h;
    }
}

使用上面的代码我收到警告“duplicate method area(double, double) in type Area."我知道如果我只是更改其中一个参数的类型我不会有这个问题,但这是唯一的方法吗完成了吗?我不能有两个具有相同参数列表的重载静态方法吗?

圆的面积可以改为半径为双精度,pi 为浮点数。这样编译器就会识别出 circle 方法的差异。本课的思路是,只要改变方法中的参数,就可以有多个同名方法,这就是方法重载的重点。

这两种方法:

  • 被称为'area'
  • 接受两个 'double'
  • 类型的参数

所以他们是无法区分的。您为正式参数选择的名称不会影响决策,因为(从琐碎的 "because that's how the language works" 开始)这些名称未在调用 area() 时写入源代码中。

鉴于问题陈述,您别无选择,只能将至少一个参数的类型更改为两个重载之一。是否允许整数大小? Float/double让我有点紧张:太容易出错了。


我知道这是 Java 中重载方法的练习,因此您必须遵循问题陈述。

但是,作为一个普遍问题:给定一个 class 命名区域和一堆静态面积计算器,IMO 命名每个这样的面积计算方法来说明它的真正含义确实更容易理解做。例如 areaOfCircleareaOfRectangle。根据参数的类型做不同的计算,好名字的样子好像不是一眼就能看懂的。

你可以做这样的事情:

public class Area {

    // Rectangle
    public static double calculateArea(int width, int length) {
        return width * length;
    }

    // Cylinder
    public static double calculateArea(double radius, int height) {
        return Math.PI * Math.pow(radius, 2) * height;
    }

    // Circle
    public static double calculateArea(double radius) {
        return Math.PI * Math.pow(radius, 2);
    }
}

然后调用它:

Area.calculateArea(2.0); // Call 'Circle' method
Area.calculateArea(2.0, 3); // Call 'Cylinder' method
Area.calculateArea(3, 5); // Call 'Rectangle' method

这是一个人为的答案,它满足了 3 个重载 'area' 方法的要求,而不必在我们可能更喜欢 double 的地方使用 float 或 int。

首先是区域class。对于每个形状,我们需要 3 个东西:一个(内部)class,它保存定义形状的值;一个传递 class 对象的函数;和(至关重要的)一个接受单个参数的 area() 方法,一个内部 class 的对象。这是满足 "overloaded method" 要求的最后一项。

class Area {

    static class Circle {
        double radius;
        Circle(double r) { radius = r; }
    }

    static Circle circle(double r) { 
        return new Circle(r);
    }

    static double area(Circle c) {
        return PI * c.radius * c.radius;
    }

    static class Rectangle {
        double length, width;
        Rectangle(double l, double w) { length = l; width = w; }
    }

    static Rectangle rectangle(double l, double w) {
        return new Rectangle(l, w);
    }

    static double area(Rectangle r) {
        return r.length * r.width;
    }

    static class Cylinder {
        double radius, height;
        Cylinder(double r, double h) { radius = r; height = h }
    }

    static Cylinder cylinder(douvle r, double h) {
        return new Cylinder(r, h);
    }

    static double area(Cylinder c) {
        return 2 * PI * c.radius * c.height + // side
               PI * c.radius * c.radius * 2;  // ends
               // see note!
    }
]

现在,如何使用它们?这些例子展示了如何:

a1 = Area.area(Area.circle(1));
a2 = Area.area(Area.rectangle(2,3));
a3 = Area.area(Area.cylinder(4,5));

像"Circle circle(...) { ... }"这样的函数存在所以我不必写"new"来创建一个Circle等等

很丑吧?

注意:您的圆柱 "area" 公式实际上是在计算体积。 3-D 实体的面积到底是什么意思?如果您指的是表面积,那么它就是两个端部圆加上环绕的矩形,构成了 'side'。前者各有面积'pi r^2';矩形的边为“2 pi r”和 'h',因此面积为“2 pi r h”。