我试图制作一个setter方法(setAvailForAssembly)来设置已组装的库存水平但由于某种原因我的条件似乎没有产生任何影响。为什么会这样?
主要类
public class TestAssembledPart {
public static void main(String args[]) {
// Constructing two Part objects (the base parts)
Part part0 = new Part("p101", "Crank", 218, 12.20);
Part part1 = new Part("p102", "Pedal", 320, 14.30);
// Constructing AssembledPart object assembled from p101 & p102
AssembledPart part2 = new AssembledPart("p183", "Crank & Pedal", 80, 3.50, part0, part1);
// replenishing stock by 100 items
part2.replenish(100);
System.out.println(part2);
// Supplying the maximum possible assembled parts - combination
// of existing parts and base parts available for assembly
System.out.println("Supplying max number of (assembled) part");
int totalAvail = part2.getStockLevel() + part2.getAvailForAssembly();
System.out.println("part2 stocklevel: " + part2.getStockLevel() + " part2 available for assembly: " + part2.getAvailForAssembly());
}
}
AssembledPart Class
public class AssembledPart extends Part {
private Part basica;
private Part basicb;
private int assembledstocklevel;
public AssembledPart(String id, String name, int stocklevel, double unitprice,
Part part0, Part part1) {
super(id, name, stocklevel, unitprice);
this.basica = part0;
this.basicb = part1;
}
public void setAvailForAssembly() {
if(basica.getStockLevel() >= basicb.getStockLevel()){
assembledstocklevel = basica.getStockLevel();
} else assembledstocklevel = basicb.getStockLevel();
}
public int getAvailForAssembly() {
return assembledstocklevel;
}
}
Part Class
public class Part {
protected int stocklevel;
public Part(String id, String name, int stocklevel, double unitprice) {
this.id = id;
this.name = name;
this.stocklevel = stocklevel;
this.unitprice = unitprice;
}
public int getStockLevel() {
return stocklevel - qty;
}
public void setStockLevel(int stocklevel) {
this.stocklevel = stocklevel;
}
}
答案 0 :(得分:1)
由于你的主要方法,你只是忘记调用for files in os.listdir("In_Files"):
print "Performing calculations on " + str(files) + " ..."
os.chdir("/home/MTS/Dropbox/dir/In_Filess")
subprocess.Popen("run_command.command -f a", shell=True) #this boots up the Perl script
time.sleep(3) #waits for the script to open...
Popen.communicate(input="1") #user inputs begin here
Popen.wait(0) #am I trying to set a return value here so that the inputs may proceed once the previous one has been accepted?
Popen.communicate(input=str(files)) #this is where the name of the file the Perl script is working on is entered
Popen.wait(0)
Popen.communicate(input="4")
Popen.wait(0)
方法,这就是因为调用gette方法得到0的原因。
此外,getter和setter通常用于访问字段,并且不提供任何其他逻辑。在你的情况下,没有参数的setter方法可能会在将来混淆某人。只需将其称为setAvailForAssembly()
或类似的内容。