Java getter和setter不工作

Java getter和setter不工作,java,getter-setter,Java,Getter Setter,我正在做一个家庭作业,在作业中我要确定圆柱体的体积。本课程的对象是类和对象。我有两个类,“CylinderTest”和“Cylinder”。气缸测试调用气缸。到目前为止,除了get和set方法外,一切似乎都在运行。我试图阻止计算负数,但这不起作用,它会执行计算 这是Cylinder测试类 public class CylinderTest { public static void main(String[] args) { Cylinder myTest = n

我正在做一个家庭作业,在作业中我要确定圆柱体的体积。本课程的对象是类和对象。我有两个类,“CylinderTest”和“Cylinder”。气缸测试调用气缸。到目前为止,除了get和set方法外,一切似乎都在运行。我试图阻止计算负数,但这不起作用,它会执行计算

这是Cylinder测试类

public class CylinderTest
{

    public static void main(String[] args)
    {
        Cylinder myTest = new Cylinder(-1, -1);
        myTest.getHeight();
        myTest.getRadius();
        System.out.println(myTest);

        printHeader();
        double volume = myTest.volume();
        displayCylinder(volume);
    }

    private static void printHeader()
    {
        System.out.println("Cylinder");
        System.out.println("________");
    }

    private static void displayCylinder(double volume)
    {
        System.out.print("Cylinder volume = ");
        System.out.println(volume);
    }
}
这是气缸类

public class Cylinder
{
    // variables
    public static final double PI = 3.14159;
    private double radius, height, volume;

    // constructor
    public Cylinder(double radius, double height)
    {
        this.radius = radius;
        this.height = height;
    }

    // Volume method to compute the volume of the cylinder
    public double volume()
    {
        return PI * radius * radius * height;
    }

    // accessors and mutators (getters and setters)
    public double getRadius()
    {
        return radius;
    }

    public void setRadius(double radius)
    {
        if (radius > 0.0)
            this.radius = radius;
        else
            this.radius = 1.0;
    }

    public double getHeight()
    {
        return height;
    }

    public void setHeight(double height)
    {
        if (height > 0.0)
            this.height = height;
        else
            this.height = 1.0;
    }

    public double getVolume()
    {
        return volume;
    }

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

}

在构造函数中,您需要使用与getter和setter中相同的测试,而不是直接设置值。当前,您使用构造函数中的
新圆柱体(-1,-1)
绕过setter中的测试,您需要使用与getter和setter中相同的测试,而不是直接设置值。目前,您可以使用
新圆柱体(-1,-1)
绕过setter中的测试。您的构造函数应该调用setter,并且您应该检查setter中的逻辑。如果调用代码传递一个负值,您真的想继续使用值1吗?

您的构造函数应该调用setter,并且您应该检查setter中的逻辑。如果调用代码传递负值,是否真的要继续使用值1?

您可以放弃构造函数并使用:

   Cylinder myTest = new Cylinder();
   myTest.setHeight(-1);
   myTest.setRadius(-1);
或者,您可以创建一个“工厂”方法:

虽然不推荐,但从语法上讲,您也可以将构造函数更改为调用setter

public Cylinder(double radius, double height)
{
  setRadius(radius);
  setHeight(height);
}

关于这被认为是不好的做法的原因,请参见:

您可以摆脱构造函数并使用:

   Cylinder myTest = new Cylinder();
   myTest.setHeight(-1);
   myTest.setRadius(-1);
或者,您可以创建一个“工厂”方法:

虽然不推荐,但从语法上讲,您也可以将构造函数更改为调用setter

public Cylinder(double radius, double height)
{
  setRadius(radius);
  setHeight(height);
}

关于这被认为是不好的做法的原因,请参见:

除了不在构造函数中执行测试之外,您也不设置卷(任何时候都为null)

因此,将构造函数更改为:

public Cylinder(double radius, double height)
{
    this.setRadius(radius);
    this.setHeight(height);
    this.volume = volume();
}

删除
setVolume()
并将
setHeight()
setRadius()
设为私有。

除了不在构造函数中执行测试外,还不设置卷(任何时候都为空)

因此,将构造函数更改为:

public Cylinder(double radius, double height)
{
    this.setRadius(radius);
    this.setHeight(height);
    this.volume = volume();
}

删除
setVolume()
并使
setHeight()
setRadius()
私有。

您的setter方法没有进行验证,因为您根本没有调用它们。正如其他人所评论的,一个好主意是在构造函数中调用它们,而不是将值直接赋给
半径
高度


像您那样初始化圆柱体属性本身并不是不正确的。但是,由于您需要运行“,您的setter方法没有进行验证,因为您根本没有调用它们。正如其他人所评论的,一个好主意是在构造函数中调用它们,而不是直接将值赋给
半径
高度


像您那样初始化圆柱体属性本身并不是不正确的。但是,因为您需要运行“您从未调用过设置程序”。而且,如果您不想允许负值,那么为什么不在构造函数中验证相同的内容呢?另外,您似乎忽略了getter的返回值。谢谢Rohit Jain。我将构造函数更改为这个公共圆柱体(双半径,双高度){setRadius(半径);setHeight(高度);}您从未调用过设置器。而且,如果您不想允许负值,那么为什么不在构造函数中验证相同的内容呢?另外,您似乎忽略了getter的返回值。谢谢Rohit Jain。我将构造函数更改为这个公共圆柱体(双半径,双高度){setRadius(半径);setHeight(高度)}嗨,戴夫,谢谢!我没有给二传打电话!哼!这看起来如何:publicstaticvoidmain(String[]args){圆柱体myTest=新圆柱体(-3,-5);System.out.println(myTest);printHeader();double volume=myTest.volume();displayclinder(volume);}//constructor公共圆柱体(双半径,双高度){//this.radius=radius;//this.height=height;setRadius(半径);设置高度(高度);}我是一个新手,所以我不知道为什么代码片段没有格式化。另外,为了回答你的另一个问题,不,如果代码传递负数,我不想继续使用值1。教授让我们这样做是为了强调安全性以及setter和getter在这方面的重要性。在现实世界中,我会使用try-catch来强制执行date Integrity Hi Dave,谢谢!我没有给setters打电话!DUH!这看起来怎么样:public static void main(String[]args){Cylinder myTest=new-Cylinder(-3,-5);System.out.println(myTest);printHeader();double volume=myTest.volume();displayclinder(volume);}//constructor public-Cylinder(双半径,双高度){//this.radius=radius;//this.height=height;setRadius(半径);setHeight(高度);}我是一个新手,所以我不知道为什么代码片段没有格式化。另外,为了回答你的另一个问题,不,如果代码传递负数,我不想继续使用值1。教授让我们这样做是为了强调安全性以及setter和getter在这方面的重要性。在现实世界中,我会使用try-catch来强制执行日期完整性