java中的不可变类及其创建规则

2020-03-07  本文已影响0人  小人物不说大话

java中的不可变类及其创建规则

不可变类顾名思义就是这个类被实例化之后不可被重新赋值,java提供的八个包装类和java.lang.String都是不可变类。

创建自定义不可变类需要遵守的规则:

1、使用private和final修饰成员变量。

2、提供带参构造方法,用于初始化成员变量。

3、不要为成员变量提供setter方法。

4、如果成员变量中有可变类时需要重写Object中的hashCode方法和equals方法。

java视频教程推荐:java视频教程

例如:创建一个不可变的Person类

publicclassPerson {

    privatefinalString Name;

    privatefinalString gender;

    /*

     * 无参构造方法

     */

    publicPerson(){

        this.Name="";

        this.gender="";

    }

    /*

     * 有参构造方法

     */

    publicPerson(String Name , String gender){

        this.Name = Name;

        this.gender = gender;

    }

    publicString getName() {

        returnName;

    }

    publicString getGender() {

        returngender;

    }

    /*

     * 重写hashCode方法

     * (non-Javadoc)

     * @see java.lang.Object#hashCode()

     */

    @Override

    publicint hashCode() {

        returnName.hashCode() + gender.hashCode();

    }

    /*

     * 重写equals方法

     * (non-Javadoc)

     * @see java.lang.Object#equals(java.lang.Object)

     */

    @Override

    publicboolean equals(Object obj) {

        if(this == obj)

            returntrue;

        if(obj != null && obj.getClass() == this.getClass()){

            Person pe = (Person)obj;

            if(this.getName().equals(pe.getName()) && this.getGender().equals(pe.getGender()))

                returntrue;

        }

        returnfalse;

    }

以上这个Person类中成员变量都是不可变类,如果其中有可变类,那么用以上的方法创建不可变类是有问题的,虽然Person的属性是不可变的,但属性引用的对象是可变的,

这样就会使Person的不可变性遭到破坏,例如如下例子。

先创建一个可变类College

publicclassCollege {

    privateString collNo;

    privateString collName;

    publicCollege(String collNo, String collName) {

        super();

        this.collNo = collNo;

        this.collName = collName;

    }

    publicCollege() {

        super();

    }

    publicString getCollNo() {

        returncollNo;

    }

    publicvoid setCollNo(String collNo) {

        this.collNo = collNo;

    }

    publicString getCollName() {

        returncollName;

    }

    publicvoid setCollName(String collName) {

        this.collName = collName;

    }

    @Override

    publicString toString() {

        return"College [collNo="+ collNo + ", collName="+ collName + "]";

    }

在Person中引用College

publicclassPerson {

    privatefinalCollege college;

    publicPerson() {

        this.college = null;

    }

    publicPerson(College college) {

        super();

        this.college = college;

    }

    publicCollege getCollege() {

        returncollege;

    }

    @Override

    publicString toString() {

        return"Person [college="+ college + "]";

    }

    publicstaticvoid main(String[] args){

        College coll = newCollege("123456","土木工程");

        Person pe = newPerson(coll);

        System.out.println("----->"+ pe);

        coll.setCollName("信息工程");                      //这样就会改变Person对象

        System.out.println("======>"+ pe);

    }

那么怎样才能创建有可变属性的不可变类呢?我们只要让程序无法访问到College属性即可

publicclassPerson {

    privatefinalCollege college;

    publicPerson() {

        this.college = null;

    }

    publicPerson(College college) {

        //创建一个和传入对象有相同属性的College,赋值给成员变量

        this.college = newCollege(college.getCollNo(),college.getCollName());

    }

    publicCollege getCollege() {

        //创建一个College将属性的值赋值给它并返回

        returnnewCollege(this.college.getCollNo(),this.college.getCollNo());

    }

    @Override

    publicString toString() {

        return"Person [college="+ college + "]";

    }

以上思路就是分离外界和Person类中可变属性的联系,程序不能直接作用于属性,这样就创建了含可变类属性的不可变类。

相关文章教程推荐:java入门教程

上一篇下一篇

猜你喜欢

热点阅读