package com.javabase.obj.comparator;
import java.util.ArrayList;
import java.util.Collections;
/**
*
* 實現深客隆方法需要實現Cloneable接口
*/
public class Student implements Cloneable{
//姓名
private String name;
//班級
private String glass;
//成績
private double core;
public Student() {
super();
}
public Student(String name, String glass, double core) {
super();
this.name = name;
this.glass = glass;
this.core = core;
}
public String getName() {
return name;
}
public String getGlass() {
return glass;
}
public double getCore() {
return core;
}
// @Override
// public String toString() {
// return this.name+"--"+this.glass+""+this.core;
// }
//重寫自定義的方法
@Override
protected Object clone() throws CloneNotSupportedException {
Student newStu = (Student) super.clone();
//newStu.name=new String(name);
newStu.glass=new String(glass);
newStu.core = new Double(core);
return newStu;
}
//測試自定義克隆方法
Student stu = new Student("水哥","三班",40);
//調用默認的克隆方法,淺克隆之後的數據是一樣的,實際上指向的還是同一個地址引用
Student clone = (Student) stu.clone();
System.out.println(clone);
System.out.println(clone.getName()==stu.getName());
System.out.println(clone.getCore()==stu.getCore());
//深客隆,深客隆后數值型的值不變
System.out.println(clone.getGlass()==stu.getGlass());
}
}