Добрый день!
Нужно сделать клон объекта класса, класс содержит 2 коллекции, каждый элемент коллекций сам является объектом.
Вот структуры классов и мой метод клонирования (который не работает):
| Код | public class Box { private Vector<Vector3D> vertexes = new Vector<Vector3D>(); private Vector<NPair> lines = new Vector<NPair>();
public Box(Vector3D A,Vector3D B,Vector3D C,Vector3D D,Vector3D E,Vector3D F,Vector3D G,Vector3D H) { this.vertexes.add(A); this.vertexes.add(B); this.vertexes.add(C); this.vertexes.add(D); this.vertexes.add(E); this.vertexes.add(F); this.vertexes.add(G); this.vertexes.add(H); lines.add(new NPair(0,1));//A->B lines.add(new NPair(1,2));//B->C lines.add(new NPair(2,3));//C->D lines.add(new NPair(3,0));//D->A lines.add(new NPair(4,5));//E->F lines.add(new NPair(5,6));//F->G lines.add(new NPair(6,7));//G->H lines.add(new NPair(7,4));//H->E lines.add(new NPair(0,4));//A->E lines.add(new NPair(1,5));//B->F lines.add(new NPair(2,6));//C->G lines.add(new NPair(3,7));//D->H }
@Override public Object clone() throws CloneNotSupportedException { return new Box(this.vertexes.get(0),this.vertexes.get(1),this.vertexes.get(2),this.vertexes.get(3),this.vertexes.get(4),this.vertexes.get(5),this.vertexes.get(6),this.vertexes.get(7)); } }
|
| Код | public class Vector3D { private double _x,_y,_z,_w; public Vector3D(double x, double y, double z) { _x = x; _y = y; _z = z; }
public Vector3D(double x, double y, double z, double w) { _x = x; _y = y; _z = z; _w = w; } }
|
| Код | public class NPair { public int n1,n2;
public NPair(int n1, int n2) { this.n1 = n1; this.n2 = n2; } }
|
Подскажите пожалуйста как правильно переопределить метод clone ? |