构造我已经把我的构造是这样的:使用“this”。在Java中
public class VendingMachine {
private double currentBalance;
private double itemPrice;
private double totalCollected;
public VendingMachine(double itemCost) {
currentBalance = 0;
totalCollected = 0;
itemPrice = itemCost;
}
...
}
我的问题是什么是从通过采取双itemCost
的参数上面设置我的构造类似的差异。
是什么,而不是使之成为区别:
this.itemPrice = itemCost;
在你的情况,没有任何区别。如果该属性与构造函数参数名称相同,则需要'this.var = var;' – jlordo
另请参见:http://stackoverflow.com/q/957502/5812 –