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
}
...
这篇关于为什么我可以更改/重新分配从类实例化的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我可以更改/重新分配从类实例化的常量值
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01