Java 17 Recipes


-10. Making a Class Cloneable


Download 3.2 Mb.
Pdf ko'rish
bet104/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   100   101   102   103   104   105   106   107   ...   245
Bog'liq
Java 17 Recipes

5-10. Making a Class Cloneable
 Problem
You want to enable a class to be cloned or copied by another class.
 Solution
Implement the Cloneable interface within the class that you want to clone; then call 
that object’s clone method to make a copy of it. The following code demonstrates how to 
make the Team class cloneable.
public class Team implements TeamType, Cloneable, Serializable {
private String name;
private String city;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
@Override
public void setName(String name) {
this.name = name;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
Chapter 5 ObjeCt-Oriented java


182
/**
* @param city the city to set
*/
@Override
public void setCity(String city) {
this.city = city;
}
public String getFullName() {
return this.name + " - " + this.city;
}
/**
* Overrides Object's clone method to create a deep copy
*
* @return
*/
@Override
public Team clone() {
Team obj = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream 
(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
obj = (Team) ois.readObject();
ois.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
Chapter 5 ObjeCt-Oriented java


183
/**
* Overrides Object's clone method to create a shallow copy
*
* @return
*/
public Team shallowCopyClone() {
try {
return (Team) super.clone();
} catch (CloneNotSupportedException ex) {
return null;
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Team) {
Team other = (Team) obj;
return other.getName().equals(this.getName())
&& other.getCity().equals(this.getCity());
} else {
return false;
}
}
}
To make a deep copy of a Team object, the clone() method needs to be called against 
that object. To make a shallow copy of the object, the shallowCopyClone() method must 
be called. The following code demonstrates these techniques.
public static void main(String[] args){
Team team1 = new Team();
Team team2 = new Team();
team1.setCity("Boston");
Chapter 5 ObjeCt-Oriented java


184
team1.setName("Bandits");
team2.setCity("Chicago");
team2.setName("Wildcats");
Team team3 = team1;
Team team4 = team2.clone();
Team team5 = team1.shallowCopyClone();
System.out.println("Team 3:");
System.out.println(team3.getCity());
System.out.println(team3.getName());
System.out.println("Team 4:");
System.out.println(team4.getCity());
System.out.println(team4.getName());
// Teams move to different cities
team1.setCity("St. Louis");
team2.setCity("Orlando");
System.out.println("Team 3:");
System.out.println(team3.getCity());
System.out.println(team3.getName());
System.out.println("Team 4:");
System.out.println(team4.getCity());
System.out.println(team4.getName());
System.out.println("Team 5:");
System.out.println(team5.getCity());
System.out.println(team5.getName());
if (team1 == team3){
System.out.println("team1 and team3 are equal");
} else {
System.out.println("team1 and team3 are NOT equal");
}
if (team1 == team5){
System.out.println("team1 and team5 are equal");
Chapter 5 ObjeCt-Oriented java


185
} else {
System.out.println("team1 and team5 are NOT equal");
}
}
This code demonstrates how to make a clone of an object. The resulting output 
would be as follows.
Team 3:
Boston
Bandits
Team 4:
Chicago
Wildcats
Team 3:
St. Louis
Bandits
Team 4:
Chicago
Wildcats
Team 5:
Boston
Bandits
team1 and team3 are equal
team1 and team5 are NOT equal

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   100   101   102   103   104   105   106   107   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling