Java中的不可變類

來源:互聯網
上載者:User

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());}}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.