Accueil
 Sommaire du cours
 1  Introduction à Java
 2  Concepts de bases de Java
 3  Classes et objets en Java
 4  Généralisation spécialisation en Java
 5  Organisation des sources Java
 6  API Java
 7  Exceptions en Java
 8  Concepts objets avancés en Java
 8.1  Copie simple/légère
 8.1.1  Copie pour studs
 8.1.2  Représentation graphique d'une copie légère dans studs
 8.1.3  Exemple de copie légère
 8.1.4  Représentation graphique d'une copie plus profonde dans studs
 8.1.5  Copie plus profonde dans studs
 8.1.6  Représentation graphique d'une copie profonde
 8.1.7  Clone de Scrutin
 8.1.8  Clone en copie profonde de Personne
 8.1.9  Suite exemple de copie profonde
 8.2  Retour sur \texttt hashCode()
 8.3  Retour sur les exceptions
 Bibliographie

 Contacts

W3C validator

Département INF  
 Conception et programmation orientées objet


8.1.5 Copie plus profonde dans studs
 
package studsclonedeep; 
import java.util.ArrayList; 
 
4import studs.Bulletin; 
public class Personne implements Cloneable { 
    private String nom, prenom; 
    private int nbParticipations = 0, nbOrg = 0; 
8    private ArrayList<Scrutin> scrutins; 
    public Personne(final String n, final String p) { 
        this.nom = n; 
        this.prenom = p; 
12        scrutins = new ArrayList<Scrutin>(); 
    } 
    public Scrutin organiserScrutin(final String nom) { 
        Scrutin s = new Scrutin(nom,this); 
16        scrutins.add(s); 
        nbOrg ++; 
        return s; 
    } 
20    /** le clonage est profond, il duplique les objets references */ 
    @SuppressWarnings("unchecked") 
    @Override 
    public Personne clone() throws CloneNotSupportedException { 
24        Personne clone = new Personne(nom, prenom); 
        clone.nbParticipations = nbParticipations; 
        clone.nbOrg = nbOrg; 
        clone.scrutins = (ArrayList<Scrutin>) scrutins.clone(); 
28        return clone; 
    } 
    public String getNom() { return nom; } 
    public String getPrenom() {return prenom; } 
32    public ArrayList<Scrutin> getScrutins() {return scrutins; } 
    public void voter(final Bulletin b){ } 
    public void consulterResultat(Scrutin s) { } 
    public void seRetirerDUnScrutin(Scrutin s) { } 
36}
 
import java.util.ArrayList; 
 
import studsclonedeep.Scrutin; 
4import studsclonedeep.Personne; 
public class TestCloneDeep { 
    public static void main(final String[] args) 
            throws CloneNotSupportedException { 
8        Personne p = new Personne("Dupont", "Julien"); 
        p.organiserScrutin("Electionbde"); 
        Personne p1 = p.clone(); 
        if (p1 == p) { 
12            System.out.println("p==p1"); 
        } 
        if (p.getNom() == p1.getNom()) { 
            System.out.println("petp1noms=="); 
16        } 
        if (p.getPrenom() == p1.getPrenom()) { 
            System.out.println("petp1prenoms=="); 
        } 
20        ArrayList<Scrutin> a1, a2; 
        a1 = p.getScrutins(); 
        a2 = p1.getScrutins(); 
        if (a1 == a2) { 
24            System.out.println("petp1scrutins=="); 
        } else { 
            for(int i = 0; i < a1.size(); i++) { 
                if(a1.get(i) == a2.get(i)) { 
28                    System.out.println("Scrutinsderang" + i +"=="); 
                } else { 
                    if(a1.get(i).getOrganisateur() == a2.get(i).getOrganisateur()) { 
                        System.out.println("Oraganisateurdesscrutinsderang" + i +"=="); 
32                    } 
                } 
            } 
        } 
36    } 
}

    précédent     suivant 


 
verbatim

Christian Bac, Denis Conan, Télécom SudParis, CSC 4002, Octobre 2015