Click here to Skip to main content
15,879,050 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi everybody,

So i have :

class Personne implements Cloneable...

The personne class uses Adress class in it's field.

private Adresse adresse=ADRESSE_UNKNOWN;


So what i want is having a option to :

modify the cloned Person adress

BUT that the original Person class won t have the access to the Cloned Person class Adress field.

Please give me a tip guys.
Posted

1 solution

Ok Guys , so with help i understood.
The possibility to override a clone() method is giving the opurtunity to modify the fields of the cloning class. As a proposed solution a reset the Adress field of the class to NULL. Then i created a setter that giving an option to modify the clone adress field.
Java
    @Override
    public Personne clone()
{
    Personne o;
    try {
       o = (Personne)super.clone();
       o.adresse = null;
       return o;
    } catch(CloneNotSupportedException cnse) {
      cnse.printStackTrace(System.err);
      throw new RuntimeException();
    }
}
 // setter pour l adresse du Clone :
    public void setAdresseClone(Adresse a){
    this.clone().adresse = a;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900