设置和获取方法

时间:2014-03-23 03:57:33

标签: java object methods get set

我有一个使用set的类,并打印出我在main中创建的对象的信息。我的问题是,我似乎无法弄清楚如何使用为我的对象输入的值在不同的方法中进行计算并打印出正确的结果。我需要将LDL除以HDL,但结果仍然是0,我的if语句仍然执行。任何人都可以指导正确的方向如何正确使用get和set方法,并将它们与其他计算方法一起使用?此外,我似乎无法理解为什么需要一个构造函数,因为在我看来,该集合做了同样的事情。下面是我的主类,用户定义的类以及执行时出现的代码。

public class Checkup {

//Fields
private int patientNumber;
private int systolic;
private int diastolic;
private double ldl;
private double hdl;
private double results;

//Constructor
public Checkup(int number, int sys, int dia, double l, double h) {

    patientNumber = number;
    systolic = sys;
    diastolic = dia;
    ldl = l;
    hdl = h;
}

//Set Methods
public void setPatientNumber(int number) {

    patientNumber = number;
}

public void setSystolic(int sys) {

    systolic = sys;
}

public void setDiastolic(int dia) {

    diastolic = dia;
}

public void setLDL(double l) {

    ldl = l;
}

public void setHDL(double h) {

    hdl = h;
}


//Get Methods
public int getPatientNumber() {

    return patientNumber;
}

public int getSystolic() {

    return systolic;
}

public int getDiastolic() {

    return diastolic;
}

public double getLDL() {

    return ldl;
}

public double getHDL() {

    return hdl;
}

//LDL and HDL calculation methods
public void setComputeRatio(double l, double h) {

    results = l / h;
}

public double getComputeRatio() {

    return results;
}

//Print Methods
public void printCheckup() {

    System.out.println(getPatientNumber());
    System.out.println(getSystolic());
    System.out.println(getDiastolic());
    System.out.println(getLDL());
    System.out.println(getHDL());
    System.out.println(getComputeRatio());
    if (getComputeRatio() <= 3.5) {

        System.out.println("HDL is known as \"Good Cholesterol\" and a ratio of 3.5 or lower          is considered optimum");
    }
    System.out.println();

}

}

这是我的主要内容:

public class TestCheckup {

    public static void main(String[] args) {

        Checkup personA = new Checkup(90880, 110, 78, 100.0, 40.0);
        Checkup personB = new Checkup(2,3,4,5.0,6.0);
        //Checkup personC = new Checkup();
        //Checkup personD = new Checkup();

        personA.printCheckup();
        personB.printCheckup();

    }
}

以及打印的内容:

90880
110
78
100.0
40.0
0.0
HDL is known as "Good Cholesterol" and a ratio of 3.5 or lower is considered optimum

2
3
4
5.0
6.0
0.0
HDL is known as "Good Cholesterol" and a ratio of 3.5 or lower is considered optimum

2 个答案:

答案 0 :(得分:0)

您为getComputeRatio()方法获得0.0的输出的原因是因为您在构造之后从未实际对results实例变量进行任何更改。在构造期间,results被隐式设置为0.0。从本质上讲,就像你的构造函数是这样编写的那样:

public Checkup(int number, int sys, int dia, double l, double h) {

    patientNumber = number;
    systolic = sys;
    diastolic = dia;
    ldl = l;
    hdl = h;
    results = 0.0; // <====
}

然而,在那之后,变量再也不会被触及......为了更好地解释我的意思,请看下面的示例代码:

public class Tester
{
    public static void main(String[] args)
    {
        // Create a new Checkup object
        Checkup personA = new Checkup(1, 2, 3, 4.0, 5.0);

        // Calculate ratio
        personA.setComputeRatio(personA.getLDL(), personA.getHDL()); // <== This is needed...

        // Get results
        personA.printCheckup(); // ... before you do this. Otherwise results will always equal 0.0
    }
}

现在你在原帖中问:

  

“任何人都可以指导如何正确使用获取和设置方法并将其与其他计算方法一起使用吗?”

实现此目的的一种方法是对您的setComputeRatio(...)方法定义进行以下更改:

public void setComputeRatio() {

    results = getLDL() / getHDL();

    /* 
    Or you could also do:
    results = ldl / hdl; 
    */   
}

上面的代码不仅实现了以前创建的get方法,还在计算中实现了。它也使事情变得不那么麻烦和尴尬,例如:

 // Calculate ratio
 personA.setComputeRatio(personA.getLDL(), personA.getHDL());

现在可以缩短为:

// Calculate ratio
 personA.setComputeRatio();

希望这有帮助!

亲切的问候, KLM

答案 1 :(得分:-1)

也许我错过了,但您在调用setComputeRatio()方法之前是否正在调用getComputeRatio()方法?