可变和不可变之间有什么区别? [英] What is the difference between mutable and immutable?

查看:91
本文介绍了可变和不可变之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我找到可变与不变之间的基本区别吗?

解决方案

不可变表示一旦初始化,对象的状态就无法更改。





例如-.NET中的字符串是不可变的。每当对字符串(修剪,大写字母等)进行操作时,都会创建一个新字符串。



实际上,如果要创建不可变的类型,则只能在其上使用吸气剂,并且不允许任何状态更改(因此,构造函数完成运行后,任何私有字段都无法更改)。


Can any one help me in finding the basic difference between mutable and immutable?

解决方案

Immutable means that once initialized, the state of an object cannot change.

Mutable means it can.

For example - strings in .NET are immutable. Whenever you do an operation on a string (trims, upper casing, etc...) a new string gets created.

In practice, if you want to create an immutable type, you only allow getters on it and do not allow any state changes (so any private field cannot change once the constructor finished running).

这篇关于可变和不可变之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆