2017-10-06 33 views
-3

好吧,所以我需要幫助,我在我的收銀機文件創建收銀java文件和retailItem文件它不能讀取我放在retailItem因爲它的私人。所以我的問題是我如何訪問我寫在我的retailItem類到收銀文件私有變量java:29:錯誤:成本已在私人存取RetailItem

因此,這裏是我的retailItem

import java.text.DecimalFormat; 
public class RetailItem 
{ 
private String description; 
private double price; 
private double unitsOnHand; 
private CostData cost; 


public void setDescription(String userDescription) 
{ 
    description = userDescription; 

} 

public void setPrice(double p) 
{ 
    price = p; 
} 


public void setUnitsOnHand(double userUnitsOnHand) 
{ 
    unitsOnHand = userUnitsOnHand; 

} 

public String getDescription() 
{ 
    return description; 
} 
public double getPrice() 
{ 
    return price; 
} 

public double getUnitsOnHand() 
{ 
    return unitsOnHand; 
} 



public RetailItem(RetailItem object2) 
{ 
    description = object2.description; 
    price = object2.price; 
    unitsOnHand = object2.unitsOnHand; 
    cost = new CostData(object2.cost.wholeSale,object2.cost.Retail); 
} 



public RetailItem(String descriptionGet,double pri, double 
unitsOnHandGet,double wholeSale,double retail) 
{ 
    description = descriptionGet; 
    price = pri; 
    unitsOnHand = unitsOnHandGet; 
    cost = new CostData(wholeSale,retail); 
} 


public String toString() 
{ 
    String str; 

    DecimalFormat dollar = new DecimalFormat("#,##0.00"); 

    str = "Description: " + description + 
     "Item Price: " + price + 
     "\nItem Number: " + unitsOnHand + 
     "\nWhole Cost: $" + dollar.format(cost.wholeSale) + 
     "\nRetail Price: $" + dollar.format(cost.Retail); 

     return str; 
} 

public class CostData 
{ 
public double wholeSale; 
public double Retail; 

public CostData(double whole,double re) 
{ 
    wholeSale = whole; 
    Retail = re; 

} 

public void setRetail(double re) 
{ 
    Retail = re; 
} 

public void setWholeSale(double whole) 
{ 
    wholeSale = whole; 
} 

public double getRetail() 
{ 
    return Retail; 
} 
public double getWholeSale() 
{ 
    return wholeSale; 
} 


}  
} 

這裏代碼收款機類:

public class CashRegister 
{ 
private RetailItem retail; 
private int quantityItem; 
private final double SALES_TAX = 0.06; 
private int subTotal; 


public CashRegister() 
{ 
    quantityItem = 0; 
    subTotal = 0; 
} 


public CashRegister(RetailItem retailObject,int quantity) 
{ 
    retail = new RetailItem(retailObject); 
    quantityItem = quantity; 
} 

public RetailItem getRetailItem() 
{ 
    return new RetailItem(retail); 
} 

public double getSubTotal() 
{ 
return quantityItem * retail.cost.getRetail();//Here is where the problem is 

} 

public double getTax() 
{ 
    return SALES_TAX; 
} 
public double getTotal() 
{ 
    return subTotal + SALES_TAX; 
} 

} 
+0

寫一個吸氣劑,就像你已經有了價格和描述。 – azurefrog

回答

0

你有多種選擇:

  1. 如果他們是在同一個包,你可以設置成本的訪問權限爲
    默認(從其前面刪除單詞private)。
  2. 將成本從private更改爲public
  3. 創建默認訪問(既不private也不publicgetCost()方法返回成本。
  4. 創建公開getCost()方法返回成本。

第一個選項提供對RetailItem相同包中任何類的讀/寫訪問權限。

第二個選項提供對所有類的讀取/寫入權限。

第四個選項提供對所有類的只讀訪問。

protected訪問也可以是一個選項。它的包訪問類似,但也提供對子類的讀/寫訪問。

另請參閱:In Java, difference between default, public, protected, and private