英文:
Boolean variable not changing when it should
问题
我正在完成一项作业任务,演示了Java中面向对象编程(OOP)的基础知识。这是一个非常简单的汽车洗车模拟项目。在`Car`类中我有两个方法,`isDirty`和`getDirty`。在汽车类中,我有一个名为`Dirty`的布尔变量,初始设置为`false`,因为所有3辆汽车都应该是干净的。我需要使用`isDirty`方法检查这3辆汽车,以证明它们初始状态都是干净的;之后,我需要运行`getDirty`方法使汽车变脏,然后再次运行`isDirty`方法,以展示状态从干净变为脏。但是由于某种原因,在运行了`getDirty`方法之后,`boolean`变量`Dirty`并没有改变为true。
```java
public class CarwashSimulation
{
Carwash suds = new Carwash();
Car carA = new Car();
Car carB = new Car();
Car carC = new Car();
public void runSimulation()
{
System.out.println("开始模拟");
carA.isDirty();
carB.isDirty();
carC.isDirty();
carA.getDirty();
carB.getDirty();
carC.getDirty();
carA.isDirty();
carB.isDirty();
carC.isDirty();
}
}
class Carwash
{
public void washCar()
{
}
}
class Car
{
boolean Dirty = false;
public void getDirty()
{
Dirty = true;
}
public boolean isDirty()
{
if (Dirty == true)
{
System.out.println("它是脏的");
return true;
}
else
{
System.out.println("它是干净的");
return false;
}
}
}
<details>
<summary>英文:</summary>
I'm currently working on a homework assignment that illustrates the basics of OOP in Java. It's a very simple carwash simulation. I have two methods in the `Car` class, `isDirty` and `getDirty`. I have a `boolean` variable `Dirty` in the car class which is set to `false` because all 3 cars are supposed to start out clean. I'm supposed to check all 3 cars with the `isDirty` method to show that all 3 cars start out clean, after, I'm supposed to run the `getDirty` method to make the cars dirty and then run the `isDirty` method to show that the status changed from clean to dirty. For some reason the `boolean` variable `Dirty` isn't changing to true after running the `getDirty` method.
public class CarwashSimulation
{
Carwash suds = new Carwash();
Car carA = new Car();
Car carB = new Car();
Car carC = new Car();
public void runSimulation()
{
System.out.println("Start Simulation");
carA.isDirty();
carB.isDirty();
carC.isDirty();
carA.getDirty();
carB.getDirty();
carC.getDirty();
carA.isDirty();
carB.isDirty();
carC.isDirty();
}
}
class Carwash
{
public void washCar()
{
}
}
class Car
{
boolean Dirty = false;
public void getDirty()
{
boolean Dirty = true;
}
public boolean isDirty()
{
if (Dirty == true)
{
System.out.println("It's Dirty");
return true;
}
else
{
System.out.println("It's Clean");
return false;
}
}
}
</details>
# 答案1
**得分**: 3
在方法 `getDirty()` 中,创建了一个新变量 `boolean Dirty` 并赋值为 `true`。然而,我们想要的是将(已存在的)字段 `Dirty` 设置为 `true`。为了实现这一点,我们只需要移除 `boolean`:
```java
class Car {
...
public void getDirty() {
Dirty = true;
}
...
}
关于代码的一些建议:
- 在Java中,字段名应始终以小写字母开头(
Dirty
->dirty
) - 方法名称中的前缀
get
通常用于表示获取方法(getter)。我建议将getDirty()
重命名为类似makeDirty()
的名称,以免用户将其误认为是一个getter方法。
英文:
In method getDirty()
, a new variable boolean Dirty
is created and assigned the value true
. However, what we want is to set the (existing) field Dirty
to true
. In order to achieve this, we only need to remove the boolean
:
class Car {
...
public void getDirty() {
Dirty = true;
}
...
}
Some remarks on the code:
- In java, field names should always start with a lowercase letter (
Dirty
->dirty
) - The prefix
get
in method names is normally used for getters. I would suggest to renamegetDirty()
to something likemakeDirty()
so that a user does not confuse it for a getter.
答案2
得分: 2
getDirty()
没有更新数据成员,而是声明了一个本地布尔变量,隐藏了数据成员,将其初始化为true
,然后在作用域结束时丢弃它。移除该声明,你应该没问题:
public void getDirty()
{
Dirty = true; // 这里!
}
附注:
如果该方法旨在设置脏状态,setDirty
可能是一个更好的名称。
英文:
getDirty()
is not updating the data member, but declaring a local boolean that hides the data member, initializes it to true
and then discards it when it goes out of scope. Remove the declaration, and you should be OK:
public void getDirty()
{
Dirty = true; // Here!
}
Side note:<br/>
If this method is supposed to set the dirty state, setDirty
is probably a better name for it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论