package d12_api_object;public class Test2 {public static void main(String[] args) throws CloneNotSupportedException {//目标:掌握Object类提供的对象克隆方法//1、protected Object clone():对象克隆User u1 = new User(1,"min","1120",new double[]{99.0,100.0});System.out.println(u1.getId());System.out.println(u1.getUsername());System.out.println(u1.getPassword());System.out.println(u1.getScores());User u2 = (User) u1.clone();System.out.println(u2.getId());System.out.println(u2.getUsername());System.out.println(u2.getPassword());System.out.println(u2.getScores());}
}
package d12_api_object;
//Cloneable是一个标记接口
//规则。
public class User implements Cloneable{private int id;//编号private String username;//用户名private String password;//密码private double[] scores;//分数public User(int id, String username, String password, double[] scores) {this.id = id;this.username = username;this.password = password;this.scores = scores;}public User() {}@Overrideprotected Object clone() throws CloneNotSupportedException {//super 去调用父类Object中的clone方法。return super.clone();}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public double[] getScores() {return scores;}public void setScores(double[] scores) {this.scores = scores;}
}
运行结果:
1
min
1120
[D@4eec7777
1
min
1120
[D@4eec7777
package d12_api_object;public class Test2 {public static void main(String[] args) throws CloneNotSupportedException {//目标:掌握Object类提供的对象克隆方法//1、protected Object clone():对象克隆User u1 = new User(1,"min","1120",new double[]{99.0,100.0});System.out.println(u1.getId());System.out.println(u1.getUsername());System.out.println(u1.getPassword());System.out.println(u1.getScores());User u2 = (User) u1.clone();System.out.println(u2.getId());System.out.println(u2.getUsername());System.out.println(u2.getPassword());System.out.println(u2.getScores());}
}
package d12_api_object;
//Cloneable是一个标记接口
//规则。
public class User implements Cloneable{private int id;//编号private String username;//用户名private String password;//密码private double[] scores;//分数public User(int id, String username, String password, double[] scores) {this.id = id;this.username = username;this.password = password;this.scores = scores;}public User() {}@Overrideprotected Object clone() throws CloneNotSupportedException {//super 去调用父类Object中的clone方法。User u2 = (User) super.clone();u2.scores = u2.scores.clone();return u2;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public double[] getScores() {return scores;}public void setScores(double[] scores) {this.scores = scores;}
}
运行结果:
1
min
1120
[D@4eec7777
1
min
1120
[D@3b07d329