您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/10/26 14:54:11发布16次查看
不可变类顾名思义就是这个类被实例化之后不可被重新赋值,java提供的八个包装类和java.lang.string都是不可变类。
创建自定义不可变类需要遵守的规则:
1、使用private和final修饰成员变量。
2、提供带参构造方法,用于初始化成员变量。
3、不要为成员变量提供setter方法。
4、如果成员变量中有可变类时需要重写object中的hashcode方法和equals方法。
java视频教程推荐:java学习
例如:创建一个不可变的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类中可变属性的联系,程序不能直接作用于属性,这样就创建了含可变类属性的不可变类。
相关文章教程推荐:java入门程序
以上就是java中的不可变类及其创建规则的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product