Why I can change/reassigned a constant value that Instantiated from a class(为什么我可以更改/重新分配从类实例化的常量值)
问题描述
我创建了以下类
class Person {
var firstName: String
var lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
func fullName() -> String {
return "(firstName) (lastName)"
}
}
然后我从类中实例化一个常量值
Then I instantiated a constant value from the class
let john = Person(firstName: "Johnny", lastName: "Applessed")
问题:为什么我可以改变变量john
的内容?不是常数吗?谁能帮我解释一下,非常感谢.
Question: Why I can change the content of the variable john
? Isn't it a constant? Can someone explain that for me, thanks a lot.
john.firstName = "John"
print(john.firstName) // -> John
推荐答案
正如 @Wain 所说 - 这是由于引用类型的性质.实例是 let
常量仅意味着您不能为其分配新的引用 - 但没有说明实例本身的实际可变性.
As @Wain has said – it's due to the nature of reference types. The instance being a let
constant only means you cannot assign a new reference to it – but says nothing about the actual mutability of the instance itself.
如果您将您的类更改为结构,您将看到 value 类型的行为有何不同,因为更改属性会更改 Person
的实际值 -因此,如果它是 let
常量,您将无法这样做.但是我有点怀疑你是否想让你的 Person
结构体,因为两个同名的人不应该被认为是同一个人.
If you change your class to a struct, you'll see how the behaviour differs with value types, as changing a property changes the actual value of your Person
– therefore you are unable to do so if it's a let
constant. However I somewhat doubt you'll want to make your Person
a struct, as two people with the same name shouldn't be considered to be the same person.
如果您只希望在初始化时分配属性(然后在实例的生命周期内为只读),那么我建议将它们设为 let
常量(而不是将它们的设置器设为私有)).这将确保一旦分配,您甚至无法在班级内更改它们的值.
If you only wish your properties to be assigned upon initialisation (and then read-only for the lifetime of the instance), then I would recommend making them let
constants (instead of making their setters private). This will ensure that you cannot even change their value from within your class, once assigned.
规则是只要你在 super.init()
调用之前给一个属性一个值——你可以把它变成一个 let
常量(在这种情况下,你只需在使用 self
之前在初始化程序中分配它们.
The rule is as long you give a property a value before the super.init()
call – you can make it a let
constant (in this case, you just have to assign them in the initialiser before using self
).
class Person {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
...
这篇关于为什么我可以更改/重新分配从类实例化的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我可以更改/重新分配从类实例化的常量值
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01