This question already has answers here:
Set and Get Methods in java?
                            
                                (15个答案)
                            
                    
                5个月前关闭。
        

    

我正在制作一个程序来计算地毯的安装成本。我正在努力为以下变量写一个setter getter消息... labourCharge,price和postCode。

最佳答案

您只需要在类内创建一个getter / setter

public class CarpetCostEstimator {
   double price = -1.0;
   String postCode = "XXX-XXX";
   double labourCharge = 1.0;

   public double getPrice() {
      return price;
   }

   public void setPrice(double price) {
      this.price = price;
   }

   public String getPostCode() {
      return postCode;
   }

   public void setPostCode(String postCode) {
      this.postCode = postCode;
   }

   public double getLabourCharge() {
      return labourCharge;
   }

   public void setLabourCharge(double labourCharge) {
      this.labourCharge = labourCharge;
   }
}

09-25 19:23