Domanda

public class Car implements Cloneable{

private String name;
private int price;

Car(String name, int price)
{
    this.name = name;
    this.price = price;
}

//copy constructor 1

Car(Car a)
{
    price = a.price;
    name = a.name;
}

clone(Car a)
{
    Car newC = Car(Car a);
}

}

Car a gives me cannot find symbol. I am trying to write a class that uses a copy constructor and a clone method, but came across an error I can't solve. I've been scratching my head for 30 minutes.

È stato utile?

Soluzione

The problem is here: Car newC = Car(Car a);

That line should be: Car newC = new Car(a);

Altri suggerimenti

You need to specify a return type, and the new keyword.

public Object clone(Car a) {
   Car newC = new Car(a);
   return newC;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top