Java 返回null的getter和setter

Java 返回null的getter和setter,java,setter,getter,Java,Setter,Getter,我正在尝试编写一个非常简单的程序,为购买的物品生成发票。 我有一个包含setter和getter的驱动程序类,还有一个应该调用它们的对象类。然而,当我打印发票时,它只会为所有内容生成空响应 这是我的驾驶课: public class Invoice { private String partNumber; //part number of item private String description; //description of item private int quantity;

我正在尝试编写一个非常简单的程序,为购买的物品生成发票。 我有一个包含setter和getter的驱动程序类,还有一个应该调用它们的对象类。然而,当我打印发票时,它只会为所有内容生成空响应

这是我的驾驶课:

public class Invoice {

private String partNumber;  //part number of item
private String description; //description of item
private int quantity;       //quantity being purchased
private double itemPrice;   //price of item

//constructor
public Invoice(String partNumber, String description, int quantity, double itemPrice) {
    partNumber = partNumber;
    description = description;
    if (quantity < 0) {
        quantity = 0;
    }
    if (itemPrice < 0.0) {
        itemPrice = 0.0;
    }
}

public void setPartNumber(String number) {
    partNumber = number; //store the partNumber
}

public void setDescription(String description) {
    description = description;//store the description
}

public void setQuantity(int quantity) {
    quantity = quantity;//store the quantity
}

public void setItemPrice(double price) {
    itemPrice = price;//store the itemPrice
}

public String getPartNumber() {
    return partNumber;//retrieve the partNumber
}

public String getDescription() {
    return description;//retrieve the description
}

public int getQuantity() {
    return quantity;//retrieve the quantity
}

public double getItemPrice() {
    return itemPrice;//retrieve the itemPrice
}

//get price for item purchased.
public double getInvoiceAmount(double amount) {
    amount = itemPrice * quantity;
    if (amount < 0) {
        amount = 0.0;
    }
    return amount;
}
}

由于以下原因,类的成员字段未按预期进行初始化:

public Invoice(String partNumber, String description, int quantity, double itemPrice)
{
    partNumber = partNumber;

这里,
partNumber
指的是函数参数,而不是成员字段。要设置成员字段,请使用
this.partNumber=partNumber

您忘记将变量分配给构造函数中的字段。使用
关键字引用类字段:

public Invoice(String partNumber, String description, int quantity, double itemPrice) {
    //here's an example
    //partNumber = partNumber
    this.partNumber = partNumber;
    //description = description;
    this.description = description;
    //similar for other assignments inside the constructor...
}
你必须在你的二传手中也这样做:

public void setPartNumber(String partNumber) {
    this.partNumber = partNumber; //store the partNumber
}

您的setter没有将值设置到对象实例中,您需要进行如下更改

public Invoice(String partNumber, String description, int quantity, 
        double itemPrice) {
  this.partNumber = partNumber;
  this.description = description;
  this.quantity = quantity;
  if (this.quantity < 0) {
    this.quantity = 0;
  }
  this.itemPrice = itemPrice;
  if (this.itemPrice < 0.0) {
    this.itemPrice = 0.0;
  }
}

public void setPartNumber(String number) {
  this.partNumber = number; //store the partNumber
}

public void setDescription(String description) {
  this.description = description;//store the description
}

public void setQuantity(int quantity) {
  this.quantity = quantity;//store the quantity
}

public void setItemPrice(double price) {
  this.itemPrice = price;//store the itemPrice
}
公共发票(字符串零件号、字符串描述、整数数量、,
双倍价格){
this.partNumber=零件号;
this.description=描述;
这个。数量=数量;
如果(该数量小于0){
这个数量=0;
}
this.itemPrice=itemPrice;
如果(此项价格<0.0){
此项价格=0.0;
}
}
public void setPartNumber(字符串编号){
this.partNumber=number;//存储partNumber
}
公共void集合描述(字符串描述){
this.description=description;//存储描述
}
公共无效设置数量(整数数量){
this.quantity=quantity;//存储数量
}
公共无效setItemPrice(双倍价格){
this.itemPrice=price;//存储itemPrice
}
public Invoice(String partNumber, String description, int quantity, 
        double itemPrice) {
  this.partNumber = partNumber;
  this.description = description;
  this.quantity = quantity;
  if (this.quantity < 0) {
    this.quantity = 0;
  }
  this.itemPrice = itemPrice;
  if (this.itemPrice < 0.0) {
    this.itemPrice = 0.0;
  }
}

public void setPartNumber(String number) {
  this.partNumber = number; //store the partNumber
}

public void setDescription(String description) {
  this.description = description;//store the description
}

public void setQuantity(int quantity) {
  this.quantity = quantity;//store the quantity
}

public void setItemPrice(double price) {
  this.itemPrice = price;//store the itemPrice
}