这是一个不可变的类吗?一个不可变的类有必要是最终的吗?
我有一个不可变的盒子类,对盒子的更改应该创建一个新盒子。
public class MyImmutableBox {
Integer length;
Integer breadth;
/*Constructor*/
public MyImmutableBox(Integer length,Integer breadth){
this.length=length;
this.breadth=breadth;
}
/*Getters*/
public Integer getBreadth() {
return breadth;
}
public Integer getLength() {
return length;
}
/*Member functions*/
/*Changing entire box size*/
public MyImmutableBox changeMyBoxSize(Integer lengthToBeAltered,Integer breadthToBeAltered){
return new MyImmutableBox(length+lengthToBeAltered,breadth+breadthToBeAltered);
}
/*Changing either length otr breadth*/
public MyImmutableBox changeMyBoxLenth(Integer lengthToBeAltered){
return new MyImmutableBox(length+lengthToBeAltered,breadth);
}
public MyImmutableBox changeMyBoxBreadth(Integer breadthToBeAltered){
return new MyImmutableBox(length,breadth+breadthToBeAltered);
}
}