Hallo.....ich hab da ein Problem bei der Deep Copy von Objekten und zwar geht es um die Klasse Color. Wenn ich das Bsp. vom Skript zur Deep Copy 1:1 abschreibe schaut das so aus:
Also meine Probleme bei der Deep Copy hängen mit Color zusammen...wie schaffe ich eine Deep Copy mit Color?
mfg,
Hannes
Code:
public class Figure implements Cloneable{
int x,y;
String name;
public Figure(int x, int y, String name){
this.x = x;
this.y = y;
this.name = name;
}
public Object clone() throws CloneNotSupportedException{
Figure f = (Figure)super.clone();
f.name = new String(name);
return f;
}
}
import java.awt.Color;
public class Circle extends Figure{
Color color;
public Circle(int x, int y, String name, Color color){
super(x,y,name);
this.color = color;
}
public Object clone() throws CloneNotSupportedException{
Circle c = (Circle)super.clone();
c.color = color.clone();
//Fehlermeldung: The method() clone from type Object is not visible
// Can't convert from Object to Color.
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Figure a = new Circle(10,20,"Hannes",red);
//Fehler......da er mir red nicht so annimmt....müsste Color.red schreiben aber dann nicht cloneable....
try{
Figure b = (Figure)a.clone();
}catch(CloneNotSupportedException e){
}
}
Also meine Probleme bei der Deep Copy hängen mit Color zusammen...wie schaffe ich eine Deep Copy mit Color?
mfg,
Hannes