Object类型的Clone()不可见

时间:2013-11-10 01:39:54

标签: java clone

这是我的计划。如果有this.pizzaorder[i].clone()this.pizzaorder[count].clone(),我会收到错误消息,指出类型Clone()中的Object不可见。有什么帮助吗?

import java.text.NumberFormat;
public class PizzaOrder {

NumberFormat moneyFormatter = NumberFormat.getCurrencyInstance();
public int TotalNoOfPizzas, keep;                                             
//declaring the variables needed for this class

public double totalcost;
Pizza[] pizzaorder = new Pizza[TotalNoOfPizzas];
int i;

public PizzaOrder() {                                              
//constructor to initialize all the varibales

    this.TotalNoOfPizzas = 0;
    this.totalcost = 0.0;
    for ( i = 1; i <= pizzaorder.length; i++ ) {
        pizzaorder[i] = null;   }
}

public PizzaOrder( PizzaOrder keep ) {                          
// copy constructor
    for ( i = 1; i <= pizzaorder.length; i++) {
        this.pizzaorder[i] = this.pizzaorder[i].clone();    }
    this.TotalNoOfPizzas = keep.TotalNoOfPizzas;
    this.totalcost = keep.totalcost;    }

public int getTotalNoOfPizzas() {                                       
//using Accessor to get the number of pizzas
    return this.TotalNoOfPizzas; }

public void setTotalNoOfPizzas( int TotalNoOfPizzas1 ) {                
//using mutators to set the number of pizzas
    this.TotalNoOfPizzas = TotalNoOfPizzas1; }

public Pizza getPizza ( int pizzas) {                                       
//using Accessor to get the size and toppings for pizza 1 
    return (this.pizzaorder[pizzas]); } 

public void setPizza(Pizza tempPizza, int count) {
    this.pizzaorder[count].clone(); }       

public double getTotalCost() {
    return(this.totalcost); }

public void setTotalCost(double totalcost1) {
    this.totalcost = totalcost1;    }



public double calcTotal() {                                                                         
//calculating the total cost
double totalcostoford = 0;
for (i = 1; i <= pizzaorder.length; i++) {
    totalcostoford += this.pizzaorder[i].calcCost();    }
return totalcostoford;  }

public String tostring() {
String pizzaDescription;
pizzaDescription = "\n Order No.:" + this.pizzaorder;
pizzaDescription +="\nTotal No. of pizza's ordered:" + TotalNoOfPizzas;
for ( i = 0; i <= pizzaorder.length; i++ ) {
    pizzaDescription += "\n" + i + ". Pizza" +       
pizzaorder[i].getPizzaDescription();  }
pizzaDescription += "\n" + moneyFormatter.format(this.getTotalCost()) + "is your     
 total price.";
return pizzaDescription;
}
}

3 个答案:

答案 0 :(得分:1)

您最初的问题是,如果您希望克隆公开显示,则需要在班级中声明public克隆方法。

但仅仅这样做是不够的:

  public Object clone() { 
     try { 
         return super.clone(); 
     } catch (Exception e) { 
         return null; 
     } 
  }

这有两个问题。首先,捕获异常并返回类似null违反了clone()预期行为的合同。阅读Object.clone()的javadoc以了解它应该如何表现。如果发生故障,没有理由返回null。您应抛出异常,或允许现有异常传播。

你获得异常的原因是javadoc解释了 。您依赖于Object的本机克隆机制。但是,只有当您尝试克隆的类实现Cloneable标记接口时,该机制才有效。如果没有,则调用super.clone()将引发CloneNotSupportedException


最后,我要指出你需要小心克隆。一方面,它可能很昂贵。另一方面,使用本机克隆机制进行克隆并不总能为您的应用程序做“正确的事情”。这种机制只给你一个浅的副本;即它不会复制您正在克隆的对象的“组件”对象。

答案 1 :(得分:0)

要克隆一个类,您需要覆盖clone()方法,并且需要将代码放在那里创建类的新实例并将其他类的成员变量复制到它,并返回新的实例

答案 2 :(得分:0)

我建议不要使用clone,而是使用复制构造函数。

Pizza类添加复制构造函数:

class Pizza {
   private final sometype some field;
   ...
   Pizza(Pizza that) {
       this.somefield = that.somefield;
   }
   ...

然后代替

this.pizzaorder[i] = keep.pizzaorder[i].clone();

DO

this.pizzaorder[i] = new Pizza(keep.pizzaorder[i]);
恕我直言,这更清晰,更容易理解。