-1

我不明白为什么我在 setInitialQuantity 方法声明中收到这些错误,有人介意看一下吗?

public class Invoice
{
    private int quantity;
    private String partNum;
    private String partDesc;
    private double unitPrice;


    //constructor 
    public Invoice ( int initialQuantity, String partNumber, String partDescript, double unitPrice)
    {
        public void setInitialQuantity ( int initialQuantity )
        {
            quantity = initialQuantity;
        }   
    }//end Invoice constructor
}

错误:

javac Invoice.java
Invoice.java:16: illegal start of expression
    public void setInitialQuantity ( int initialQuantity )
    ^
Invoice.java:16: illegal start of expression
    public void setInitialQuantity ( int initialQuantity )
           ^
Invoice.java:16: ';' expected
    public void setInitialQuantity ( int initialQuantity )
                                  ^
Invoice.java:16: ';' expected
    public void setInitialQuantity ( int initialQuantity )
                                                        ^
4 errors
4

5 回答 5

1

您不能在 Java 中的另一个(或构造函数)中拥有一个方法,将其移到构造函数之外但在类中。

public class Invoice
{
    private int quantity;
    private String partNum;
    private String partDesc;
    private double unitPrice;


    //constructor 
    public Invoice ( int initialQuantity, String partNumber, String partDescript, double unitPrice)
    {
    }//end Invoice constructor

public void setInitialQuantity ( int initialQuantity )
    {
            quantity = initialQuantity;
    }   

}
于 2012-09-10T05:24:45.283 回答
1

您不能在另一个方法中定义方法。

//constructor     
public Invoice(int initialQuantity,String partNumber,String partDescript,double unitPrice)
{
   this.quantity=initialQuantity;
   this.partNum=partNumber;
   this.partDesc=partDescript;
   this.unitPrice=unitPrice;
}   
public void setInitialQuantity(int initialQuantity)
{
    quantity = initialQuantity;
} 
于 2012-09-10T05:25:56.240 回答
0

将 Cosntructor 更改为:

 public Invoice ( int initialQuantity, String partNumber, String partDescript, double unitPrice)
    {      
       quantity = initialQuantity;         
    }//end Invoice constructor
于 2012-09-10T05:26:14.800 回答
0

您不能在构造函数中使用方法,可能是您正在寻找类似的东西:

public class Invoice
{
    private int quantity;
    private String partNum;
    private String partDesc;
    private double unitPrice;


    //constructor 
    public Invoice ( int initialQuantity, String partNumber, String partDescript, double unitPrice)
    {
        quantity = initialQuantity;
        partNum = partNumber;
        partDesc = partDescript;
        unitPrice = unitPrice;
    }//end Invoice constructor

    public void setInitialQuantity ( int initialQuantity )
    {
        quantity = initialQuantity;
    }
}
于 2012-09-10T05:35:59.467 回答
0

您应该在构造函数之外定义“setInitialQuantity”,然后从构造函数中调用它。像这样的东西:

//constructor          
public Invoice (int initialQuantity, String partNumber, String partDescript, double unitPrice)
{   
   setInitialQuantity(initialQuantity);     
}//end Invoice constructor

public void setInitialQuantity (int initialQuantity)
{
   quantity = initialQuantity;
} 
于 2012-09-10T05:37:20.030 回答