我正在制作一个程序,计算安装地毯要花费多少。我已经创建了变量,因此具有双重价格,双重收费和字符串邮政编码。如何编辑CarpetEstimator,以使其采用单个double参数并将实例变量labourCharge设置为该参数的值,并将price和postCode分别设置为-1.0和“ XXX XXX”。
public CarpetCostEstimator()
{
// initialise instance variables
double price = -1.0;
postCode =XXX-XXX;
到目前为止我做了什么非常感谢
Java中的构造函数可以在方法签名的括号内定义parameters。如果您将它们命名为与实例变量相同的名称,请确保使用前缀this.
引用instance variables
在您的情况下,像这样添加单个参数double labourCharge
:
public class CarpetCostEstimator {
// define instance variables
double price;
double labourCharge;
String postCode;
public CarpetCostEstimator(double labourCharge) {
// initialise instance variables
this.price = -1.0; // set using default
this.postCode = "XXX-XXX"; // set using default
this.labourCharge = labourCharge; // set using parameter
}
// define other methods here
}