当我尝试编译此程序时,错误指向“new”一词。我正在尝试从carOrder类创建2个对象,我很烦恼!我以前在其他程序中遇到过这个问题,我不知道为什么,它会杀了我,请帮帮忙!
import java.text.DecimalFormat;
import java.util.Scanner;
public class CarOrder
private String buyer;
private String carType;
private double cost;
private int quantity;
private boolean taxStatus;
private double discountedCost;
private double taxAmount;
// Default Constructor
public void carOrder()
{
}
// Constructor
public void CarOrder(String buy, String car, double cos, int quan, boolean tax)
{
buyer = buy;
carType = car;
cost = cos;
quantity = quan;
taxStatus = tax;
}
// Sets the company buying cars
public void setBuyer(String buy)
{
buyer = buy;
}
// Sets the car type being purchased
public void setCarType(String car)
{
carType = car;
}
// Sets cost of the cars being purchased
public void setCost(double cos)
{
cost = cos;
}
// Sets the quantity of cars being purchased
public void setQuantity(int quan)
{
quantity = quan;
}
// Sets tax status for the cars
public void setTaxStatus(boolean tax)
{
taxStatus = tax;
}
// Returns name of buyer to user
public String getBuyer()
{
return buyer;
}
// Returns type of car to user
public String getCarType()
{
return carType;
}
// Returns cost to user
public double getCost()
{
return cost;
}
// Returns quantity of cars to user
public int getQuantity()
{
return quantity;
}
// Returns tax status to user
public boolean getTaxStatus()
{
return taxStatus;
}
// Returns discounted cost to user
public double getDiscountedCost()
{
if (quantity > 10)
if (quantity > 20)
discountedCost = cost - cost * .10;
else
discountedCost = cost - cost * .05;
else
discountedCost = cost;
return discountedCost;
}
// Returns tax amount to users
public double getTaxAmount()
{
taxAmount = cost * .0625;
return taxAmount;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
CarOrder speedy = new CarOrder("Speedy Rental", "Mini Cooper", 22150, 15, true);
CarOrder zip = new CarOrder("Zip Car Co.", "Ford Fusion", 27495, 6, true);
System.out.println("Enter first Buyer");
String buyer1 = keyboard.nextLine();
}
}
答案 0 :(得分:6)
public void CarOrder(String buy, String car, double cos, int quan, boolean tax)
{
应该是
public CarOrder(String buy, String car, double cos, int quan, boolean tax)
{
构造函数没有返回类型,甚至没有返回。
目前,您的类中有一个名为CarOrder
的方法,因为它的返回类型为void,这会消除custructor的规则。如果你删除void,那么它就是一个构造函数,因为它与你的类名相同。
同样适用于没有argsaswell的构造函数。
public void CarOrder()
应该是
public CarOrder()
答案 1 :(得分:1)
你在公共类CarOrder之后错过了一个“{”......:)
答案 2 :(得分:0)
当您不声明构造函数时,Java提供了一个没有参数的默认构造函数。当您声明CarOrder(String buy, String car, double cos, int quan, boolean tax)
时,不再创建默认构造函数。你创建了一个名为carOrder
的方法,这可能是试图使构造函数没有参数,但它有两个问题:
如果您想进行new CarOrder()
来电,只需添加以下代码:
public CarOrder() {
//insert your implementation here
}
答案 3 :(得分:0)
具有返回类型的构造函数被编译器视为方法。