2

这是我的程序。无论何时有this.pizzaorder[i].clone()or 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;
}
}
4

3 回答 3

1

public您最初的问题是,如果您希望克隆公开可见,则需要在类中声明克隆方法。

但仅仅这样做是不够的:

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

这有两个问题。首先,捕获异常并以这种方式返回null违反预期clone()行为的约定。阅读 javadocObject.clone()以了解它的行为方式。null如果发生故障,没有理由返回。您应该抛出异常,或允许现有异常传播。

javadoc也解释了您遇到异常的原因。您依赖于 Object 的本机克隆机制。但该机制仅在您尝试克隆的类实现Cloneable标记接口时才有效。如果没有,那么跟注super.clone()会加注CloneNotSupportedException


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

于 2013-11-10T02:09:53.923 回答
0

要克隆一个类,您需要重写 clone() 方法,并且您需要将代码放入其中,以创建您的类的新实例并将另一个类的成员变量复制到它,并返回新实例。

于 2013-11-10T01:58:44.993 回答
0

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

Pizza类中,添加一个复制构造函数:

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

然后代替

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

this.pizzaorder[i] = new Pizza(keep.pizzaorder[i]);

恕我直言,这更清晰,更容易理解。

于 2013-11-10T02:27:43.153 回答