我正在创建一个程序,该程序的机队具有诸如载重量和重量限制之类的规格。有需要进入飞机的物品的货物清单。我有货运课,飞机课和主装载机课。我想让飞机上放一堆货物。我觉得我已经很接近完成这项工作,但是我可能会缺少一些东西。我在将getter和setter的语法与push和pop结合使用时遇到麻烦。
package sandbox;
import java.util.Stack;
public class StacksMain {
public static void main(String[] args) {
Airplane aircraft = new Airplane();
Cargo exampleLoad = new Cargo(1, 2, 3, 4);
Cargo exampleLoad2 = new Cargo(1, 5, 3, 4);
// test stack
Stack<Cargo> testStack = new Stack<Cargo>();
// set cargo in current aircraft
aircraft.setCurrentCargo(testStack);
// push new cargo into aircraft
//this is where i'm running into the issue
aircraft.setCurrentCargo().push(exampleLoad2);
testStack.push(exampleLoad);
System.out.println(testStack.pop().getValue());
}
}
package sandbox;
public class Cargo {
private int trackingNum;
private int cubicFeet;
private int value;
private int weight;
public Cargo(int trackingNum, int cubicFeet, int value, int weight) {
setTrackingNum(trackingNum);
setCubicFeet(cubicFeet);
setValue(value);
setWeight(weight);
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getTrackingNum() {
return trackingNum;
}
public void setTrackingNum(int trackingNum) {
this.trackingNum = trackingNum;
}
public int getCubicFeet() {
return cubicFeet;
}
public void setCubicFeet(int cubicFeet) {
this.cubicFeet = cubicFeet;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
package sandbox;
import java.util.Stack;
public class Airplane {
private Stack<Cargo> currentCargo;
public Airplane() {
setCurrentCargo(currentCargo);
}
public Stack<Cargo> getCurrentCargo() {
return currentCargo;
}
public void setCurrentCargo(Stack<Cargo> currentCargo) {
this.currentCargo = currentCargo;
}
}
答案 0 :(得分:0)
您的setCurrentCargo()
方法是void
,因此您无法在该方法的结果上调用push
。您也不需要;这个
aircraft.setCurrentCargo(testStack);
// push new cargo into aircraft
//this is where i'm running into the issue
aircraft.setCurrentCargo().push(exampleLoad2);
可能是
aircraft.setCurrentCargo(testStack);
// push new cargo into aircraft
aircraft.getCurrentCargo().push(exampleLoad2);
但是,因为您只是将货物设置为testStack
,所以我希望
aircraft.setCurrentCargo(testStack);
// push new cargo into aircraft
testStack.push(exampleLoad2);
答案 1 :(得分:0)
您好,您只需将aircraft.setCurrentCargo().push(exampleLoad2);
替换为aircraft.getCurrentCargo().push(exampleLoad2);
。