Java 与构造函数中参数传递的混淆

标签 java variables constructor

我正在创建一个 BMR 计算器,并且有一个名为 User 的类。该类包含用于计算 BMR 的所有方法,以及将用户数据(年龄、性别、体重、高度)打包在一起的构造函数。

代码是:

public class User {

int age;
String gender; // todo: use an Enum
double height; // height stored in cm, weight in kg (so if user enters in feet/lbs, conversions are done to cm/kg and *THEN* passed through to constructor below)
double weight;
double activityMultiplier; // todo: use an Enum  (possibly)
int bmr;


public User(int age, String gender, double height, double weight,
    double activityMultiplier) {
    this.age = age;
    this.gender = gender;
    this.height = height;
    this.weight = weight;
    this.activityMultiplier = activityMultiplier;

    bmr = calcBMR();
}

/**
 * If user input is correct, this method will calculate the BMR value of the user given their input and measurement choices.
 * 
 * @param None
 * @return BMR Value
 */
public int calcBMR() {
    int offset = gender.equals("M") ? 5 : -161;
    // This is the body of the calculations - different offset used depending on gender. Conversions to kg and cm done earlier so no conversions needed here.
    // The formula for male and female is similar - only the offset is different.
    return (int) (Math.round((10 * weight) + (6.25 * height) - (5 * age) + offset)); // This is the Miffin St-Jeor formula, calculations done in cm/kg
    }

/**
 * If the user selects the TDEE option, this method will be executed after the calcBMR() method. 
 * A value from the calcBMR() method will be passed down to this method, and is multiplied
 * by the activity level parameter passed into this method.
 * 
 * @param bmr (output from calcBMR() method
 * @return TDEE Value
 */
public int calcTDEE(int bmr) {
    return (int) Math.round(calcBMR() * activityMultiplier);
}

}

我担心的是,我不确定在构造函数中初始化 bmr 值的方式 (bmr = calcBMR()) 是否正确。我无法计算 bmr,直到用户的年龄、性别、高度和体重被记录并存储在变量中(这就是上面 5 行所做的)。这样的编程结构好吗? IE。当创建 User 对象时,年龄、性别、高度和体重存储在变量中,然后在构造函数中调用一个方法来计算和存储另一个值。

有更好的方法吗?如果没有,我需要执行 this.bmr = calcBMR() 还是 bmr = calcBMR() 可以吗?

请注意,User 对象是在单独的类中创建的。我感到困惑的原因主要是因为我没有将 bmr 参数传递到构造函数中,而是使用方法返回值来启动实例变量的值。

最佳答案

语法上没问题,但是您不应该从构造函数中调用可重写(公共(public)/ protected 非最终)方法。如果有人覆盖它,它可能会扰乱你的对象的构造。从构造函数调用辅助方法很好,只需将其设为私有(private)或最终即可。

this.bmr = calcBMR()bmr = calcBMR()

bmr.calcBMR() 没有意义,因为 calcBMR 方法位于 User 对象上。 bmr 是一个 int,因此它没有名为 calcBMR

的方法

是否使用这个取决于您的偏好。只有当您有一个也名为 bmr 的局部变量,然后您显式调用实例变量而不是局部变量时,它才会真正产生影响。一般来说,具有相同名称的本地变量和实例变量会令人困惑。

您的 calcTDEE 方法有点不对劲。您可以只使用 bmr 的值,而不将其传入或重新计算它,因此它是

public int calcTDEE() {
    return (int) Math.round(bmr * activityMultiplier);
}

关于Java 与构造函数中参数传递的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32388791/

相关文章:

python - python中一个输入的两个值?

c++ - 了解构造函数概念

java - 为什么 ThreadLocalRandom 类中的某些方法不在 Random 类中?

ruby - 如何清除 ruby​​ 中 rspec 测试之间的类变量

C : variable global error at compilation : can not be used when making a shared project

java - Getter 方法返回错误值

javascript - 从 String 实例化对象时不想使用 eval - 帮助?

java - 将字符串压缩为a2b3...等

java - SortedSet 的元素类型允许计算给定值的后继

java - 如何仅重新加载部分代码 - Java