Java set方法不起作用

时间:2014-07-24 07:00:43

标签: java

您好我是编程新手,也是这里的第一次海报。我无法让Java应用程序显示通过公共类中的Set方法分配的正确值。具体来说,CreatePurchase应用程序为所有3个用户定义的变量(invoiceNumber,saleAmount,salesTax)返回0,这些变量在Purchase公共类中设置。

在此设置并显示值

public class Purchase
{
private int invoiceNumber;
private double saleAmount;
private double salesTax;
public int getInvoiceNumber()
{
    return invoiceNumber;
}
public void setInvoiceNumber(int inv)
{
    inv = invoiceNumber;
}
public double getSaleAmount()
{
    return saleAmount;
}
public void setSaleAmount(double sale)
{
    sale = saleAmount;
}
public double getSalesTax()
{
    return salesTax;
}   
public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    tax = salesTax;
}  
public static void displayPurchase(Purchase aPurch)
{
    System.out.println("Purchase invoice number is " + aPurch.getInvoiceNumber() + "and the sale amount is " + aPurch.getSaleAmount() + "the taxable amount is " +aPurch.getSalesTax());
}
}

这是CreatePurchase类,它提示用户输入变量并调用方法来显示新对象的值

import java.util.Scanner;
public class CreatePurchase
{
public static void main(String args[])
{
  Purchase aPurchase;
  aPurchase = getPurchaseInfo();
  Purchase.displayPurchase(aPurchase);
}
public static Purchase getPurchaseInfo()
{
  Purchase tempPur = new Purchase();
  int invoice;
  double value;
  double value2;
  Scanner input = new Scanner(System.in);
  System.out.println("Enter the invoice number:");
  invoice = input.nextInt();
  while(invoice < 1000 || invoice > 8000)
  {
     System.out.println("You made an invalid selection");
     System.out.println("You entered " + invoice);
     System.out.println("Please enter a whole number between 1000 and 8000");
     invoice = input.nextInt();
  }
  tempPur.setInvoiceNumber(invoice);
  System.out.println("Enter the amount of the sale:");
  value = input.nextDouble();
  value2 = (value * .05);
  while(value < 0)
  {
     System.out.println("You made an invalid selection");
     System.out.println("You entered " + value);
     System.out.println("Please enter a non negative number");
     value = input.nextDouble();
     value2 = (value *.05);
  }
  tempPur.setSaleAmount(value);
  tempPur.setSalesTax(value2);
  return tempPur;
  }
}

非常感谢任何关于如何正确设置和显示输入值的方向或建议。

5 个答案:

答案 0 :(得分:3)

setter需要将新值分配给实例字段。

public void setSaleAmount(double sale)
{
   sale = saleAmount;
}

你现在正好相反,转换任务:

public void setSaleAmount(final double sale)
{
   this.saleAmount = sale;
}

您还可以选择将final添加到参数中(因为您不打算对其进行更改),并使用this.清除实例字段的内容。纯粹是可选的,但是很好的做法,在这种情况下,任何一次添加都会导致编译时错误,以提醒您错误。

答案 1 :(得分:2)

您的作业有误(对于制定者)。

设置为salesTax

public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    tax = salesTax; /* wrong assignment(for the purpose) */
}

等等你的其他类变量。

请记住:SettersGetters用于修改类的变量。

答案 2 :(得分:1)

所有订阅者都需要修改如下

public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    //tax = salesTax; <-- This line is wrong..
    this.salesTax= tax ;  // Change this line and it should work...
}

public void setInvoiceNumber(int inv)
{
    //inv = invoiceNumber ; --> Invalid setting
    this.invoiceNumber = inv; // Correct setting
}

public void setSaleAmount(double sale)
{
    //sale = saleAmount;  --> Invalid setting
    this.saleAmount = sale; // Correct setting
}

<强>原因:

tax是局部变量,它特定于方法setSalesTax。

salesTax是对象的全局变量。

在对象中设置值时,需要在全局变量salesTax中设置/ assign

为避免混淆,使用setter的最佳方式将是: 对象类:

private double amount;
public void setAmount (double amount)
{
   this.amount = amount;
}

答案 3 :(得分:1)

这是因为您没有将新值设置为对象值。

正确的方法是

public void setSalesTax(double tax)
{
    tax = saleAmount *.05;
    salexTax = tax; //THIS is the mistake
}  

每当您想要为Object,Integer或其他任何内容赋值时,它都是从左到右。所以,如果你写:

int value=5;
int value2=10;
value2=value;

那么Value2是5。

答案 4 :(得分:0)

你的setter方法错了。您必须在LHS上使用实际变量而不是正式参数。

对于Ex,setInvoceNumber方法应如下所示:

public void setInvoiceNumber(int inv)
{
     invoiceNumber = inv ;
}

这里inv是正式参数,而invoiceNumber是实际变量,它将把你的值存储在object中。您将invoiceNumber的值分配给inv变量,这对您的代码没有任何影响。所有你的setter方法也是如此。