/** * Returns the name of the entity (class, interface, array class, * primitive type, or void) represented by this {@code Class} object, * as a {@code String}. */
public String getName() { String name = this.name; if (name == null) this.name = name = getName0(); return name; }
==: 基本類型,比較的是值是否相同;引用類型,比較的是地址值是否相同ide
equals(): 只能比較引用類型,默認狀況下,比較的是地址值是否相同;重寫後比較的是對象中的值this
public class A implements Cloneable { private String content; private String time; public A() { } public A(String content, String time) { this.content = content; this.time = time; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; A a = (A) o; return Objects.equals(content, a.content) && Objects.equals(time, a.time); } @Override public int hashCode() { return Objects.hash(content, time); } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return "A{" + "content='" + content + '\'' + ", time='" + time + '\'' + '}'; } }
public class ATest { public static void main(String[] args) { A a = new A("開發", "8"); try { Object cloneA = a.clone(); // A{content='開發', time='8'} System.out.println(cloneA); A oldA = a; oldA.setContent("分類"); oldA.setTime("6"); // A{content='分類', time='6'} System.out.println(a); // A{content='分類', time='6'} System.out.println(oldA); // A{content='開發', time='8'} System.out.println(cloneA); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
Object 克隆是淺克隆spa
class B { private String play; public B() { } public B(String play) { this.play = play; } public String getPlay() { return play; } public void setPlay(String play) { this.play = play; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; B b = (B) o; return Objects.equals(play, b.play); } @Override public int hashCode() { return Objects.hash(play); } @Override public String toString() { return "B{" + "play='" + play + '\'' + '}'; } } public class A implements Cloneable { private B b; private String content; private String time; public A() { } public A(B b, String content, String time) { this.b = b; this.content = content; this.time = time; } public B getB() { return b; } public void setB(B b) { this.b = b; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; A a = (A) o; return Objects.equals(b, a.b) && Objects.equals(content, a.content) && Objects.equals(time, a.time); } @Override public int hashCode() { return Objects.hash(b, content, time); } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return "A{" + "b=" + b + ", content='" + content + '\'' + ", time='" + time + '\'' + '}'; } }
public class ATest { public static void main(String[] args) { B b = new B("Game"); A a = new A(b,"開發", "8"); try { A cloneA = (A)a.clone(); // false System.out.println(a == cloneA); // true System.out.println(b == cloneA.getB()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
深度克隆code
class B implements Cloneable{ private String play; public B() { } public B(String play) { this.play = play; } public String getPlay() { return play; } public void setPlay(String play) { this.play = play; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; B b = (B) o; return Objects.equals(play, b.play); } @Override public int hashCode() { return Objects.hash(play); } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return "B{" + "play='" + play + '\'' + '}'; } } public class A implements Cloneable { private B b; private String content; private String time; public A() { } public A(B b, String content, String time) { this.b = b; this.content = content; this.time = time; } public B getB() { return b; } public void setB(B b) { this.b = b; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; A a = (A) o; return Objects.equals(b, a.b) && Objects.equals(content, a.content) && Objects.equals(time, a.time); } @Override public int hashCode() { return Objects.hash(b, content, time); } @Override protected Object clone() throws CloneNotSupportedException { A clone = (A)super.clone(); // 引用對象也拷貝一份 clone.b = (B) b.clone(); return clone; } @Override public String toString() { return "A{" + "b=" + b + ", content='" + content + '\'' + ", time='" + time + '\'' + '}'; } }
public class ATest { public static void main(String[] args) { B b = new B("Game"); A a = new A(b,"開發", "8"); try { A cloneA = (A)a.clone(); // false System.out.println(a == cloneA); // false System.out.println(b == cloneA.getB()); // B{play='Game'} System.out.println(b); // B{play='Game'} System.out.println(cloneA.getB()); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }