除了getPrice()
我返回的最后一种方法外,一切都运行良好,int
但我一直收到同样的错误。此外,如果我将保修设置为假,它仍会返回 (base+((base/100)*10))
public class Machinery extends SaleGroup {
private float serial;
public int base;
private static boolean hasWarranty;
public Machinery(String newItemDescription, float newProductCode,
float newSerial, int newBasePrice) {
super(newItemDescription, newProductCode);
serial = newSerial;
base = newBasePrice;
}
public boolean IncludeWarranty() {
return hasWarranty=true;
}
public boolean ExcludeWarranty() {
return hasWarranty=false;
}
public float getSerial() {
return serial;
}
public int getPrice()
{
if (hasWarranty==true)
{
return (base+((base/100)*10));
}
else if (hasWarranty==false)
{
return base;
}
}
}
我有 3 个类,SaleGroup.java、 Machinery.java 和 MachineryTest.java
public abstract class SaleGroup {
private String item;
private float code;
//Constructor with name and code parameters for specifying
//access methods that return the name and code
public SaleGroup(String newItemDescription, float newProductCode)
{
item = newItemDescription;
code = newProductCode;
}
public String getItemDescription()
{
return item;
}
public float getProductCode()
{
return code;
}
public abstract int getPrice();
public String toString()
{
return "Item " + item + "has product code " + code + " and price is" + getPrice();
}
}
机械测试.java
import javax.swing.JOptionPane;
public class MachineryTest {
public static void main(String[] args) {
String newItemDescription = "Item";
float newSerial = 4234;
float newProductCode = 3424;
int newBasePrice = 1000;
boolean hasWarranty=true;
Machinery test1 = new Machinery(newItemDescription, newProductCode,
newSerial, newBasePrice);
JOptionPane.showMessageDialog(
null,
"Item: " + test1.getItemDescription() + " Serial: "
+ test1.getSerial() + " Code: "
+ test1.getProductCode() + " Warranty Included: "
+ hasWarranty + " Price " + test1.getPrice());
}
}
*更新:*
除了我返回 int 的最后一个方法 getPrice() 之外,一切都工作正常,但我一直收到同样的错误。此外,如果我将保修设置为假,它仍会返回 (base+((base/100)*10))