Java实现Immutable Class要点
来源:互联网 发布:mp280清零软件 编辑:程序博客网 时间:2024/06/07 18:43
Java中很多class都是immutable,像String,Integer等,它们通常用来作为Map的key.
那么在实现自定义的Immutable的Class的时候,应该注意哪些要点呢?
a)Class 应该定义成final,避免被继承。
b)所有的成员变量应该被定义成final。
c)不要提供可以改变类状态(成员变量)的方法。【get 方法不要把类里的成员变量让外部客服端引用,当需要访问成员变量时,返回成员变量的copy】
d)构造函数不要引用外部可变对象。如果需要引用外部可以变量,应该在构造函数里进行defensive copy。
- Wrong way to write a constructor:
- public final class MyImmutable {
- private final int[] myArray;
- public MyImmutable(int[] anArray) {
- this.myArray = anArray; // wrong
- }
- public String toString() {
- StringBuffer sb = new StringBuffer("Numbers are: ");
- for (int i = 0; i < myArray.length; i++) {
- sb.append(myArray[i] + " ");
- }
- return sb.toString();
- }
- }
- // the caller could change the array after calling the
- constructor.
- int[] array = {1,2};
- MyImmutable myImmutableRef = new MyImmutable(array) ;
- System.out.println("Before constructing " + myImmutableRef);
- array[1] = 5; // change (i.e. mutate) the element
- System.out.println("After constructing " + myImmutableRef);
- Out put:
- Before constructing Numbers are: 1 2
- After constructing Numbers are: 1 5
- Right way to write an immutable class
- Right way is to copy the array before assigning in the constructor.
- public final class MyImmutable {
- private final int[] myArray;
- public MyImmutable(int[] anArray) {
- this.myArray = anArray.clone(); // defensive copy
- }
- public String toString() {
- StringBuffer sb = new StringBuffer("Numbers are: ");
- for (int i = 0; i < myArray.length; i++) {
- sb.append(myArray[i] + " ");
- }
- return sb.toString();
- }
- }
- // the caller cannot change the array after calling the constructor.
- int[] array = {1,2};
- MyImmutable myImmutableRef = new MyImmutable(array) ;
- System.out.println("Before constructing " + myImmutableRef);
- array[1] = 5; // change (i.e. mutate) the element
- System.out.println("After constructing " + myImmutableRef);
- Out put:
- Before constructing Numbers are: 1 2
- After constructing Numbers are: 1 2
- Java实现Immutable Class要点
- Java实现Immutable Class要点
- [ZZ]Java实现Immutable Class要点
- Java实现Immutable Class(不可变类)要点
- Immutable class
- How to make a java class immutable
- Java Immutable Class:如何创建,有哪些优点
- How to create a immutable class in java
- 【Java】【容易忽略的知识点】不可变类 (IMMUTABLE CLASS)
- 不可变类(immutable class)
- How to create Immutable Class and Object in Java - Tutorial Example
- java string is immutable
- Java String is "immutable"
- Java 编译后的class文件覆盖注意要点
- 不可变类(immutable class)
- How to write an immutable Class?
- React Redux router Immutable Es6等框架学习要点整理
- immutable和mutable对象(java)
- 安卓开发之常见死机问题--log分析
- js实现字符串转换成unicode
- Android的jni的调用C,C++的几个应用(基本类型,数组,类(结构体))文档-源码
- arraycopy效率比较
- MtVerify.h 头文件内容,用的时候添加到工程就行了
- Java实现Immutable Class要点
- 关于wp7中http的GetResponse抛异常的问题
- android四大组件的 基本知识
- error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
- 如何将Android应用程序编译至image/rom中
- 类似BeanUtils.copyProperties拷贝对象属性值的方法
- 最短路
- 中国内地开源镜像站汇总
- C# 获取文件信息并导出Excel,Xml报表