java中的不可變類


  不可變類顧名思義就是這個類被實例化之后不可被重新賦值,java提供的八個包裝類和java.lang.String都是不可變類。

創建自定義不可變類需要遵守的規則:

  1、使用private和final修飾成員變量。

  2、提供帶參構造方法,用於初始化成員變量。

  3、不要為成員變量提供setter方法。

  4、如果成員變量中有可變類時需要重寫Object中的hashCode方法和equals方法。

  例如創建一個不可變的Person類

  

public class Person {

    private final String Name;
    
    private final String gender;
    
    /*
     * 無參構造方法
     */
    public Person(){
        this.Name="";
        this.gender="";
    }
    
    /*
     * 有參構造方法
     */
    public Person(String Name , String gender){
        this.Name = Name;
        this.gender = gender;
    }

    public String getName() {
        return Name;
    }

    public String getGender() {
        return gender;
    }

    /*
     * 重寫hashCode方法
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return Name.hashCode() + gender.hashCode();
    }

    /*
     * 重寫equals方法
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if(this == obj)
            return true;
        if(obj != null && obj.getClass() == this.getClass()){
            Person pe = (Person)obj;
            if(this.getName().equals(pe.getName()) && this.getGender().equals(pe.getGender()))
                return true;
        }
        return false;
    }
    

 

以上這個Person類中成員變量都是不可變類,如果其中有可變類,那么用以上的方法創建不可變類是有問題的,雖然Person的屬性是不可變的,但屬性引用的對象是可變的,

這樣就會使Person的不可變性遭到破壞,例如如下例子

先創建一個可變類College

public class College {

    private String collNo;
    
    private String collName;

    public College(String collNo, String collName) {
        super();
        this.collNo = collNo;
        this.collName = collName;
    }

    public College() {
        super();
    }

    public String getCollNo() {
        return collNo;
    }

    public void setCollNo(String collNo) {
        this.collNo = collNo;
    }

    public String getCollName() {
        return collName;
    }

    public void setCollName(String collName) {
        this.collName = collName;
    }

    @Override
    public String toString() {
        return "College [collNo=" + collNo + ", collName=" + collName + "]";
    }

在Person中引用College

public class Person {

    private final College college;

    public Person() {
        this.college = null;
    }

    public Person(College college) {
        super();
        this.college = college;
    }

    public College getCollege() {
        return college;
    }
    
    
    @Override
    public String toString() {
        return "Person [college=" + college + "]";
    }

    public static void main(String[] args){
        College coll = new College("123456","土木工程");
        Person pe = new Person(coll);
        System.out.println("----->" + pe);
        coll.setCollName("信息工程");                      //這樣就會改變Person對象
        System.out.println("======>" + pe);
    }

那么怎樣才能創建有可變屬性的不可變類呢?我們只要讓程序無法訪問到College屬性即可

public class Person {

    private final College college;

    public Person() {
        this.college = null;
    }

    public Person(College college) {
        //創建一個和傳入對象有相同屬性的College,賦值給成員變量
        this.college = new College(college.getCollNo(),college.getCollName());
    }

    public College getCollege() {
        //創建一個College將屬性的值賦值給它並返回
        return new College(this.college.getCollNo(),this.college.getCollNo());
    }
    
    
    @Override
    public String toString() {
        return "Person [college=" + college + "]";
    }

以上思路就是分離外界和Person類中可變屬性的聯系,是的程序不能直接作用於屬性,這樣就創建了含可變類屬性的不可變類

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM