2013-02-24 47 views
0

当我尝试编译该程序时,出现错误指向“新”字样。我正在尝试从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

构造函数没有返回类型。 (void) – 2013-02-24 22:23:57

+0

//默认构造函数 public void carOrder()不是构造函数 – DominikM 2013-02-24 22:24:38

回答

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() 
+0

非常感谢! – Matt 2013-02-24 22:25:39

+0

@ user2089277欢迎您。请让它接受答案.. :) – PermGenError 2013-02-24 22:41:37

1

你缺少一个 “{” 公共类CarOrder之后... :)

0

当你不声明构造,Java提供了一个默认没有参数的构造函数。正如你声明的CarOrder(String buy, String car, double cos, int quan, boolean tax),默认的构造函数不再被创建。你犯了一个叫carOrder方法,这可能是企图使不带参数的构造函数,但它有两个问题:

  • 它有一个返回类型(无效),并构造没有一个
  • 名称是不同类(cardOrder是不一样的CarOrder,因为Java是区分大小写)

如果你想使一个new CarOrder()电话,只需添加以下代码:

public CarOrder() { 
    //insert your implementation here 
} 
0

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

相关问题