Cloning a class

1

I'm "trying" by cloning the following class:

public class CadHorario implements Serializable, Cloneable {

    private int cdHorario;
    ...
    private Date horarioInicio;
    private Date horarioFim;
    private DiasDaSemana diasDaSemana;
    ...

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

Class DaysDay Week:

public class DiasDaSemana implements Cloneable {

    private boolean seg;
    private boolean ter;
    private boolean qua;
    private boolean qui;
    private boolean sex;
    private boolean sab;
    private boolean dom;
    ...

    @Override
    protected Object clone() throws CloneNotSupportedException {
         return super.clone();
    }
}

Some attribute is cloning, but others like DiasDaSemana is not cloning. I'm cloning like this:

CadHorario clonado = (CadHorario) horario.clone();

Could anyone help me?

    
asked by anonymous 27.11.2015 / 13:42

1 answer

0

Looking at what you've done, there's nothing wrong. Only two observations: I understand that it would not be necessary to implement the Cloneable interface in the class DaysDay Week; and by convention would make the following adaptation in the clone method:

@Override
public CadHorario clone() throws CloneNotSupportedException {
   return (CadHorario) super.clone();
}

If an attribute of the class has not been cloned, it is probably because it was not previously instantiated.

Finally, if you still can not solve the problem, you can use libraries such as BeansUtils (Apache commons) or yourself to implement a constructor for this, eg

public CadHorario(CadHorario horario){
   this.cHorario = horario.cHorario;
   ...
}

CadHorario clone = new CadHorario(horario);
    
27.11.2015 / 15:39