gpt4 book ai didi

java - 为什么现在要从我的类中创建对象

转载 作者:行者123 更新时间:2023-12-01 07:33:36 26 4
gpt4 key购买 nike

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

}

最佳答案

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

Constructor's don't have a return type, not even void.

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

这同样适用于带有 no-argsaswell 的构造函数。

public void CarOrder()

应该是

public  CarOrder()

关于java - 为什么现在要从我的类中创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15057374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com