如何为实例变量设置默认值? [英] How do I set default values for instance variables?

查看:86
本文介绍了如何为实例变量设置默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的作业中,我有一个 Food 类,如下所示:

<块引用>

class 食物 {私人字符串名称;//食物名称私人 int calPerServing;//每份卡路里//必须 >= 0//最低存储温度//必须 >= 0.//最大存储温度//必须 <= 100.私人浮动 minTemp;私人浮动 maxTemp;int getCalories() {返回 calPerServing;}}

我在这部分作业中遇到了问题:

<块引用>

Food类编写一个构造方法,设置默认name"Pie",默认calPerServing500,默认 minTemp25,默认 maxTemp75.

如何为 namecalPerServing 等设置默认值?

解决方案

(我很惊讶到了搜索结果的第三页却没有找到这个问题的简单版本来指向.)

<小时>

设置实例变量(有时称为实例字段")的默认值有两种选择:

  1. 在声明中使用初始值设定项.

  2. 在构造函数中使用赋值.

假设我有一个类 Example 和一个实例变量 answer,其默认值应该是 42.我可以:

//#1 的示例(初始化程序)类示例{私人 int 答案 = 42;//...}

//#2 的例子(在构造函数中赋值)类示例{私人回答;例子() {this.answer = 42;}}

在构造函数中的任何其他逻辑之前(但在调用超类构造函数之后)在构造期间处理初始化程序(#1).

您使用哪种取决于班级,并在一定程度上取决于个人风格.如果该类将有多个构造函数,但我希望 answer 默认为 42,而不管使用哪个构造函数,则使用初始化是有意义的,因为我只是把它放在一个地方而不是把它放在每个地方构造函数.如果 answer 的默认值会因使用的构造函数或构造函数接收/接收的参数而异,则在构造函数中设置它是有意义的.

在您的特定情况下,赋值会告诉您使用构造函数.

In my assignment, I'm given a Food class like this:

class Food {
    private String name; // name of food
    private int calPerServing; // calories per serving
    // must be >= 0
    // min storage temperature // must be >= 0.
    // max storage temperature // must be <= 100.
    private float minTemp; private float maxTemp;
    int getCalories() {
        return calPerServing;
    }
}

And I'm having trouble with this part of the assignment:

Write a constructor method for the Food class, to set the default name to "Pie", the default calPerServing to 500, the default minTemp to 25, and the default maxTemp to 75.

How do I set default values for name, calPerServing, etc.?

解决方案

(I'm surprised to get to page three of search results without finding a simple version of this question to point to.)


You have two choices for setting the default value of an instance variable (sometimes called an "instance field"):

  1. Using an initializer on the declaration.

  2. Using an assignment in a constructor.

Say I have a class Example and an instance variable answer whose default should be 42. I can either:

// Example of #1 (initializer)
class Example {
    private int answer = 42;
    // ...
}

or

// Example of #2 (assignment within constructor)
class Example {
    private int answer;

    Example() {
        this.answer = 42;
    }
}

Initializers (#1) are processed during construction just before any other logic in your constructor (but after calling the superclass constructor).

Which you use depends on the class and, to an extent, personal style. If the class is going to have multiple constructors but I want answer to default to 42 regardless of which constructor is used, using initialization makes sense because then I just put it in one place instead of putting it in every constructor. If the default for answer will vary depending on which constructor is used, or depending on a parameter the constructor(s) receives/receive, setting it in the constructor makes sense.

In your specific case, the assignment tells you to use a constructor.

这篇关于如何为实例变量设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆