0

当我尝试编译这个程序时,一个错误指向“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();
}

}

4

4 回答 4

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)
{

构造函数没有返回类型,甚至没有 void。

目前,您有一个CarOrder在您的类中命名的方法,因为它的返回类型为 void,这违反了 custructor 的规则。如果您删除 void,那么它将是一个构造函数,因为它与您的类同名。

这同样适用于您的无参数的构造函数。

public void CarOrder()

应该

public  CarOrder()
于 2013-02-24T22:24:10.757 回答
1

您在公共课 CarOrder 之后缺少一个“{”... :)

于 2013-02-24T22:24:17.860 回答
0

当您不声明构造函数时,Java 提供了一个没有参数的默认构造函数。正如您声明CarOrder(String buy, String car, double cos, int quan, boolean tax)的那样,不再创建默认构造函数。您创建了一个名为 的方法carOrder,这可能是尝试创建一个不带参数的构造函数,但它有两个问题:

  • 它有一个返回类型 (void) 而构造函数没有
  • 名称与类不同(cardOrder 与 CarOrder 不同,因为 Java 区分大小写)

如果要new CarOrder()拨打电话,只需添加以下代码:

public CarOrder() {
    //insert your implementation here
}
于 2013-02-24T22:27:46.227 回答
0

具有返回类型的构造函数被编译器视为方法。

于 2013-02-24T22:28:24.300 回答