Tec&Cult
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Tec&Cult

Technologie et Culture
 
AccueilAccueil  PortailPortail  GalerieGalerie  Dernières imagesDernières images  RechercherRechercher  S'enregistrerS'enregistrer  Connexion  
Sujets similaires
Rechercher
 
 

Résultats par :
 
Rechercher Recherche avancée
Derniers sujets
» Que faire à Paris le week-end du 15 août ?
Clone Java objects EmptyJeu 11 Aoû - 11:20 par Admin

» T-shirt Mec de Paname
Clone Java objects EmptyMer 3 Aoû - 17:04 par Admin

» dating web site uk dating free parent single
Clone Java objects EmptyMer 3 Aoû - 10:56 par Invité

» rsvp dating website seeking bisexual
Clone Java objects EmptyLun 1 Aoû - 2:08 par Invité

» dating french woman gay bottom seeking tops
Clone Java objects EmptyDim 31 Juil - 21:59 par Invité

» dating philippine woman man seeking wealthy woman
Clone Java objects EmptyVen 29 Juil - 12:51 par Invité

» dating lesbian n r cacee cobb dating lachey nick
Clone Java objects EmptyVen 29 Juil - 3:17 par Invité

» single dating chat room relationship dating advice
Clone Java objects EmptyJeu 28 Juil - 0:21 par Invité

» скачать порно тетя скачать порнофото семейное
Clone Java objects EmptyJeu 21 Juil - 14:34 par Invité

Navigation
 Portail
 Index
 Membres
 Profil
 FAQ
 Rechercher
Forum
Partenaires
Forum gratuit


Tchat Blablaland
Le deal à ne pas rater :
Cartes Pokémon : la prochaine extension Pokémon sera EV6.5 Fable ...
Voir le deal

Partagez | 
 

 Clone Java objects

Voir le sujet précédent Voir le sujet suivant Aller en bas 
AuteurMessage
Admin
Admin


Messages : 156
Date d'inscription : 20/05/2010

Clone Java objects _
MessageSujet: Clone Java objects   Clone Java objects EmptyJeu 23 Juin - 16:02

http://toptech.geekaddict.net/
Clone Java objects
Since objects in Java are manipulated through reference variables, there is no direct way to copy an object. Classes that want copying functionality must implement clone() method to do so. This guide shows how to make a Java class Cloneable and perform cloning.
A class to be cloned:
class Person {
private String firstName;
private String lastName;

private Person assistant;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public void setAssistant(Person assistant) {
this.assistant = assistant;
}

public Person getAssistant() {
return assistant;
}
}
Todos:
implementing the Cloneable interface
make Object clone() method public (by overriding the protected method)
call the clone method defined in Object base class (to create a shallow copy of the object)
deep copy (clone) all mutable objects in case a reference copy is not enough
Our cloneable class:
class Person implements Cloneable {
private String firstName;
private String lastName;

private Person assistant;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public void setAssistant(Person assistant) {
this.assistant = assistant;
}

public Person getAssistant() {
return assistant;
}

public Object clone() throws CloneNotSupportedException {
Person cloned = (Person) super.clone();

/*
If we don't do anything else, the cloned person
will refer to the to the same assistant object
as the original person.

In this example we want to avoid that and make a real copy
of the assistant object as well.
*/
if (this.assistant != null)
cloned.assistant = (Person) this.assistant.clone();
return cloned;
}
}
Note: deep copying can be risky if there is a circular reference as it can produce an endless loop of cloning. Those cases need special attention. In this concrete example it's not a problem, we assumed that the "assistant" relationship is not reflexive.
How to clone:
public class Test {
public static void main(String[] args) {
try {
Person john = new Person("John", "Doe");
Person bob = new Person("Bob", "Gombocki");
john.setAssistant(bob);
Person johnsClone = (Person) john.clone();
System.out.println("John is cloned: " + (john != johnsClone));
System.out.println("John's assistant, Bob is cloned: " + (john.getAssistant() != johnsClone.getAssistant()));
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
To Clone or NOT to Clone
This article focused on how to use object cloning to copy objects. But it's important to know its flaws - discussed in greater detail by Josh Bloch - and understand why it's usually not the right thing to do.
I also recommend Venkat Subramaniam's article on copy constructors, and how to use them in conjunction with cloning.
Revenir en haut Aller en bas
http://toptech.geekaddict.net
 

Clone Java objects

Voir le sujet précédent Voir le sujet suivant Revenir en haut 
Page 1 sur 1

Permission de ce forum:Vous ne pouvez pas répondre aux sujets dans ce forum
Tec&Cult :: Informatique :: Java :: Tutoriel-
Sauter vers: