Java初学者:球体class

Java beginner: Sphere class

我正在为下面发布的代码寻求帮助。这是一个问题,我必须制作一个球体 class 和另一个 class 来测试它。我几乎什么都明白了,但唯一让我难受的是设置新的直径和获得新的体积。当我设置一个新直径时直径很容易改变,但是当我再次尝试 运行 体积时,它只使用旧直径而不是新直径。请帮忙谢谢!

public class Sphere {
private double sphDiam, volume, surfArea;
private final double VOL_RELAY = 4.0 / 3.0;
private final int SURF_CONST = 4;

public Sphere(double sphDiam) {
    this.sphDiam = sphDiam;
    setVolume();
    setSurfaceArea();
}

public double getDiam() {
    return sphDiam;
}

public void setDiam(double sphDiam) {
    this.sphDiam = sphDiam;
}

public double getVolume() {
    return volume;
}

public void setVolume() {
    volume = Math.pow(sphDiam / 2, 3) * Math.PI * VOL_RELAY;
}

public double getSurfaceArea() {
    return surfArea;
}

public void setSurfaceArea() {
    surfArea = Math.pow(sphDiam / 2, 2) * Math.PI * SURF_CONST;
}

public String toString() {
    return "Sphere diameter: " + sphDiam + "\nSphere Volume: " + volume + "\nSphere Surface Area: " + surfArea; 
}
}

public class MultiSphere {

public static void main(String[] args) {

    Sphere sph1 = new Sphere(10.5);
    Sphere sph2 = new Sphere(8.4);
    Sphere sph3 = new Sphere(20.1);

    sph1.setDiam(3.2);
    System.out.println(sph1.getDiam());
    System.out.println(sph1.getVolume());
    System.out.println(sph1.getSurfaceArea());
    System.out.println(sph1);
    System.out.println();
    sph1.setDiam(2.5);
    System.out.println(sph1.getDiam());
    System.out.println(sph1.getVolume());
    System.out.println(sph1.getSurfaceArea());
    System.out.println(sph1);
    System.out.println();


    System.out.println(sph2.getDiam());
    System.out.println(sph2.getVolume());
    System.out.println(sph2.getSurfaceArea());
    System.out.println(sph2);
    System.out.println();

    System.out.println(sph3.getDiam());
    System.out.println(sph3.getVolume());
    System.out.println(sph3.getSurfaceArea());
    System.out.println(sph3);
    System.out.println();
}
}

当您 "set the diameter" 时,您正在更改形状的尺寸。但是您从未更新过音量。所以 volume 值仍然反映了之前的维度。

我想您的 setter 应该只更新该值。像这样:

public void setDiam(double sphDiam) {
    this.sphDiam = sphDiam;
    setVolume();
}

但是... 如果你更进一步,你可以稍微简化你的对象。查看您的 setVolume() 方法...它不接受值。它实际上并没有设置任何东西,它只是重新计算一个动态值。该值根本不需要存储,只需在 getter.

中计算即可

所以完全去掉 volume 变量,完全去掉 setVolume() 方法,把计算放在 getter:

public double getVolume() {
    return Math.pow(sphDiam / 2, 3) * Math.PI * VOL_RELAY;
}

对任何其他计算值重复上述步骤。

无需存储易于计算的值,因为这样您就承担了保持值同步的责任。这就是导致您出现此问题的原因。球体需要的唯一值是半径(或直径)。所有其他值都来自于此。如果您要存储其他值,那么实际上您是在多个位置存储相同的信息。最好尽可能避免这种情况。

您只需在创建球体时设置体积和表面。 如果您不告诉您的程序执行此操作,则设置新直径不会自动重新评估体积和表面。

有2种解决方案:

  • 每次更改直径时重新计算表面和体积:

    public Sphere(double sphDiam) {
        this.sphDiam = sphDiam;
        setVolume();
        setSurfaceArea();
    }
    
    public double getDiam() {
        return sphDiam;
    }
    
    public void setDiam(double sphDiam) {
        this.sphDiam = sphDiam;
        setVolume();
        setSurfaceArea();
    }
    
    public double getVolume() {
        return volume;
    }
    
    public void setVolume() {
        volume = Math.pow(sphDiam / 2, 3) * Math.PI * VOL_RELAY;
    }
    
    public double getSurfaceArea() {
        return surfArea;
    }
    
    public void setSurfaceArea() {
        surfArea = Math.pow(sphDiam / 2, 2) * Math.PI * SURF_CONST;
    }
    
  • 每次获取值时计算值,这样,您可以删除变量 volumesurface 以及相关的设置器:

    public Sphere(double sphDiam) {
        this.sphDiam = sphDiam;
    }
    
    public double getDiam() {
        return sphDiam;
    }
    
    public void setDiam(double sphDiam) {
        this.sphDiam = sphDiam;
        setVolume();
        setSurfaceArea();
    }
    
    public double getVolume() {
        return Math.pow(sphDiam / 2, 3) * Math.PI * VOL_RELAY;
    }
    
    public double getSurfaceArea() {
        return Math.pow(sphDiam / 2, 2) * Math.PI * SURF_CONST;
    }