package com.home;public class Address {private final String detail;public Address() {this.detail = "";}public Address(String detail) {this.detail = detail;}public String getDetail() {return detail;}@Overridepublic int hashCode() {return detail.hashCode();}@Overridepublic boolean equals(Object obj) {if (obj instanceof Address) {Address address = (Address) obj;if (this.getDetail().equals(address.getDetail())) {return true;}}return false;}}
package com.home;public class Person {private final Name name;public Person(Name name) {super();this.name = name;}public Name getName() {return name;}public static void main(String[] args) {Name n = new Name("三", "張");Person p = new Person(n);System.out.println(p.getName().getFirstName());// 改變Person對象Name屬性的firstName屬性值n.setFirstName("無忌");System.out.println(p.getName().getFirstName());}}
Name:
package com.home;public class Name {private String firstName;private String lastName;public Name() {super();}public Name(String firstName, String lastName) {super();this.firstName = firstName;this.lastName = lastName;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}}
package com.home;public class Person {private final Name name;public Person(Name name) {super();// 設定name屬性為臨時建立的Name對象,該對象的firstName屬性和lastName屬性// 與傳入的name對象的firstName屬性和lastName屬性相同this.name = new Name(name.getFirstName(), name.getLastName());}public Name getName() {// 返回一個匿名對象,該對象的firstName屬性和lastName屬性// 與該對象裡的name屬性的firstName屬性和lastName屬性相同return new Name(name.getFirstName(), name.getLastName());}public static void main(String[] args) {Name n = new Name("三", "張");Person p = new Person(n);System.out.println(p.getName().getFirstName());// 改變Person對象Name屬性的firstName屬性值n.setFirstName("無忌");System.out.println(p.getName().getFirstName());}}
package com.home;public class CacheImmutale {private final String name;private static CacheImmutale[] cache = new CacheImmutale[10];private static int pos = 0;public CacheImmutale(String name) {super();this.name = name;}public String getName() {return name;}public static CacheImmutale valueOf(String name) {// 遍曆已緩衝的對象for (int i = 0; i < pos; i++) {// 如果已有相同執行個體,直接返回該緩衝的執行個體if (cache[i] != null && cache[i].getName().equals(name)) {return cache[i];}}// 如果緩衝池已滿if (pos == 10) {// 把緩衝的第一個對象覆蓋cache[0] = new CacheImmutale(name);pos = 1;return cache[0];} else {// 把新建立的對象緩衝起來,pos加1cache[pos++] = new CacheImmutale(name);return cache[pos - 1];}}@Overridepublic int hashCode() {return name.hashCode();}@Overridepublic boolean equals(Object obj) {if (obj instanceof CacheImmutale) {CacheImmutale ci = (CacheImmutale) obj;if (name.equals(ci.getName())) {return true;}}return false;}public static void main(String[] args) {CacheImmutale c1 = CacheImmutale.valueOf("hello");CacheImmutale c2 = CacheImmutale.valueOf("hello");System.out.println(c1 == c2);// 輸出結果為true}}
package com.home;public class Person {private final Name name;public Person(Name name) {super();// 設定name屬性為臨時建立的Name對象,該對象的firstName屬性和lastName屬性// 與傳入的name對象的firstName屬性和lastName屬性相同this.name = new Name(name.getFirstName(), name.getLastName());}public Name getName() {// 直接返回當前執行個體的name屬性即可return name;}public static void main(String[] args) {Name n = new Name("三", "張");Person p = new Person(n);System.out.println(p.getName() + " " + p.getName().getFirstName());// 改變Person對象Name屬性的firstName屬性值n.setFirstName("無忌");System.out.println(p.getName() + " " + p.getName().getFirstName());}}