如何使用List <>成员不可变的类?(How to make a class with List<> member immutable?)
例如
class School { public List<Student> Students {get; private set;} }
这里
School
不是不变的,因为Students
是一个可变的收藏。 如何使该类不可变?For example
class School { public List<Student> Students {get; private set;} }
Here
School
is not immutable because the getterStudents
is a mutable collection. How to make the class immutable?
更新时间:2023-02-04 12:02
最满意答案
相反,您可以公开一个不可变列表 :
class School { private readonly List<Student> _students = new List<Student>(); public ReadOnlyCollection<Student> Students { get { return _students.AsReadOnly(); } } }
当然,这样做对
Student
对象仍然没有影响,因此为了完全不变,Student
对象需要是不可变的。You could just expose an immutable list instead:
class School { private readonly List<Student> _students = new List<Student>(); public ReadOnlyCollection<Student> Students { get { return _students.AsReadOnly(); } } }
Of course doing this still has no impact on the
Student
objects, so to be completely immutable, theStudent
objects would need to be immutable to.
相关问答
更多-
你应该使用lazy collection来创建无限的集合。 你可以使用Stream : def loopStream: Stream[Int] = { lazy val loop: Stream[Int] = 3 #:: 4 #:: 5 #:: 6 #:: 7 #:: 8 #:: loop 1 #:: 2 #:: loop } loopStream.take(22).toList // List(1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6, 7, 8, 3, 4, 5, ...
-
在akka / scala中的演员之外访问不可变的成员(Access an immutable member outside of an actor in akka/scala)[2021-09-17]
那么,这只是我的看法。 我认为你不应该按照你的建议去做,因为那样会使你的代码变得“多样化”(我的意思是,从演员模型变成更具体的东西)。 从技术上讲,演员不应该分享任何状态,而且他们应该只对事件(消息)做出反应。 无论如何,用于理解你可以将上面的内容重写为: for { room <- rooms id <- (room ? GetId).mapTo[Int] name <- (room ? GetName).mapTo[String] } { println(id) println(na ... -
我认为你在正确的轨道上 - 注入类的所有信息都应该在构造函数中提供 所有属性都应该只是吸嘴 如果一个集合(或数组)被传递到构造函数中,则应该复制一个集合(或者Array)来保持调用者稍后修改它 如果要返回您的集合,则返回副本或只读版本(例如,使用ArrayList.ReadOnly或类似的 - 可以将其与上一点组合,并存储只读副本以返回,调用者访问它),返回枚举器,或使用允许只读访问集合的其他方法/属性 请记住,如果您的任何成员是可变的,您仍然可以出现可变类 - 如果是这种情况,您应该将任何要保留的状态复制 ...
-
删除null元素后java8 make immutable List(java8 make immutable List after removing null elements)[2022-03-09]
使用Collections.unmodifiableList来包装由流收集的List 。 list = Collections.unmodifiableList(list.stream() .filter(Objects::nonNull) .collect(Collectors.toList())); Collectors.toList()方法执 ... -
人是不可变的吗? 不,不是。 不可变类是final ,只有final成员。 在您的情况下,您要使用的是构建器类: final Person person = new PersonBuilder().withFather(xx).withMother(xx).build(); 通过这种方式,您可以使Person所有成员都成为final ,并且由于Person本身是final,因此您将获得一个真正的不可变类。 Is Person immutable? No it isn't. An immutable cla ...
-
尽管有可变成员,我怎样才能让这个类不可变?(How can I make this class immutable despite having a mutable member?)[2022-06-18]
如果您使用Eclipse集合 ,则可以将MyImmutableClass.data的类型MyImmutableClass.data为ImmutableList 。 class MyImmutableClass { private ImmutableListdata; public MyImmutableClass(List data) { this.data = Lists.immutable.withAll(data); } ... -
如果您尝试实际运行您的代码: let x = Immutable.List([Immutable.Record({ name: 'Steve' })]) console.log('x', x) // Spoiler alert: NOT undefined 你会发现它输出 x List [ function Re ...
-
相反,您可以公开一个不可变列表 : class School { private readonly List
_students = new List (); public ReadOnlyCollection Students { get { return _students.AsReadOnly(); } } } 当然,这样做对Student对象仍然没有影响,因此为了完全不变, Student对象需要 ... -
不可变类不是非常不可变的(Immutable classes not so immutable)[2023-02-02]
“final”属性只影响对列表的引用,而不影响列表本身。 如果你想要一个不可变的列表, List对象本身必须是不可变的。 谢天谢地,这里有一些标准的解决方案: Collections.unmodifiableList()是一个用于使列表不可变的标准Java包装器 谷歌的番石榴不可移动列表 (这是最方便的选项恕我直言),或 实现自己的列表,创建后无法更改 The "final" attribute affects only the reference to the list, not the list its ... -
这与我记得的一个老问题非常相似; 我再也找不到了,所以我试图重建这个方法: trait Base { protected def contribute : List[String] = Nil val list = contribute } trait TrA extends Base { override protected def contribute = "A" :: super.contribute } trait TrB extends Base { override p ...