java:29:错误:cost在RetailItem

时间:2017-10-06 18:20:58

标签: java

好吧我在这里需要帮助我在我的收银机文件中创建了一个收银机java文件和retailItem文件,它无法读取我放在retailItem中的部分,因为它是私有的。所以我的问题是我如何访问我在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;
}

}

1 个答案:

答案 0 :(得分:0)

您有多种选择:

  1. 如果它们位于同一个包中,您可以将成本访问权限设置为
    默认(从前面删除单词private。)
  2. 将费用从private更改为public
  3. 创建默认访问权限(privatepublicgetCost()方法以返回费用。
  4. 创建公开getCost()方法以返回费用。
  5. 第一个选项提供与RetailItem在同一个包中的任何类的读/写访问权限。

    第二个选项提供对所有类的读/写访问权。

    第四个选项提供对所有类的只读访问权限。

    protected访问权限也可以是一种选择。它类似于包访问,但也提供对子类的读/写访问。

    另请参阅:In Java, difference between default, public, protected, and private