gpt4 book ai didi

java - 从 Engine 类获取变量数据在 Java 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:14 27 4
gpt4 key购买 nike

我的项目是组合,其中一个卡车有一个引擎,可以使用控制台中的参数来调用:

(注意,engineStatus代表两个参数,这两个参数将决定参数,IE,将输入一个或另一个;On/Off或True/False)

PrimaTruck truck = new PrimaTruck(150, OUColour.RED, (engineStatus));

我不知道如何让我的第二个类获取engineStatus()并将变量正确插入到构造函数中以与上面的控制台命令代码一起使用。我的主要错误是它找不到 engineStatus 变量。

我也承认我的代码对于 Engine boolean 表达式来说是错误的,并且无法找出一个好的替代方案来为 engineStatus() 提供一个 On 或 Off 变量,具体取决于 engineStart() 和 engineStop() 是否打开/关闭,从而给出一个可以在 PrimaTrucks() 构造函数参数中输入的值。

PrimaTruck.Java

import ou.*;
/**
* Write a description of class PrimaTruck here.
*/
public class PrimaTruck{
// instance variables - replace the example below with your own
private int topSpeed;
private OUColour colour;
public Engine engineStatus;

/**
* Constructor for objects of class PrimaTruck
* Initialise instance variables
*/
public PrimaTruck(int topSpeed, OUColour colour, Engine engineStatus){

this.topSpeed = topSpeed;
this.colour = colour;
this.engineStatus = engineStatus;
}

public void setColour(OUColour aColour){
this.colour = aColour;
}

public OUColour getColour(){
return colour;
}

public int getTopSpeed(){
return topSpeed;
}
}

Engine.Java

/**
* Write a description of class Engine here.
*/
public class Engine{

// instance variables
public Boolean engineStart;
public Boolean engineStop;
public String engineStatus;
private Boolean status;

/**
* Constructor for objects of class Engine
*/
public Engine(String engineStatus){

// initialise instance variables
this.engineStart = engineStart;
this.engineStop = engineStop;
this.engineStatus = engineStatus;
this.status = status;
}

public Boolean status(){
if (engineStart == true){
status = engineStart;
}else if (engineStop = true){;
status = engineStop;
}
return this.status;
}

public Boolean getEngineStart(){
return engineStart;
}

public void setEngineStart(){
this.engineStop = false;
this.engineStart = true;
}

public Boolean getEngineStop(){
return engineStop;
}

public void setEngineStop(){
this.engineStart = false;
this.engineStop = true;
}

public void setEngineStatus(String aStatus){
this.engineStatus = aStatus;
}

public String getEngineStatus(){
return engineStatus;
}
}

任何有关编码的反馈或有关如何实现的指导将不胜感激。

最佳答案

Engine 类中的很多代码看起来都是多余的。我正在对代码进行很多更改。看看这是否是您想要的。

public class Engine{

// instance variables
public boolean engineStart;
public boolean engineStop;
public boolean engineStatus;

/**
* Constructor for objects of class Engine
*@param1 - true if engine is started
*@param2 - true if engine is stopped
*@param3 - true if engine is on, else false
*/
public Engine(boolean engineStart, boolean engineStop, boolean engineStatus){

// initialise instance variables
this.engineStart = engineStart;
this.engineStop = engineStop;
this.engineStatus = engineStatus;

}

public boolean getEngineStart(){
return engineStart;
}

public void setEngineStart(){
this.engineStop = false;
this.engineStart = true;
setEngineStatus(true);
}

public boolean getEngineStop(){
return engineStop;
}

public void setEngineStop(){
this.engineStart = false;
this.engineStop = true;
setEngineStatus(false);
}

public void setEngineStatus(boolean engineStatus){

this.engineStatus = engineStatus;

}

public String getEngineStatus(){
return engineStatus;
}
}

这个Engine类只有3个 boolean 变量:engineStart、engineStop、engineStatus。如果engineStart = true,则engineStatus = true。否则engineStatus = false。

我希望这是您想要实现的功能。现在,您需要在创建 Engine 对象时传入 3 个 boolean 变量。

现在在卡车类中进行以下更改:

public class PrimaTruck{
// instance variables - replace the example below with your own
private int topSpeed;
private OUColour colour;
public boolean engineStatus;

/**
* Constructor for objects of class PrimaTruck
* Initialise instance variables
*/
public PrimaTruck(int topSpeed, OUColour colour, Engine engine){

this.topSpeed = topSpeed;
this.colour = colour;
this.engineStatus = engine.getEngineStatus();
}

如果这不是您想要的,请随时在评论中询问。

关于java - 从 Engine 类获取变量数据在 Java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41696150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com