为什么我可以更改/重新分配从类实例化的常量值

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
    }

...

这篇关于为什么我可以更改/重新分配从类实例化的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么我可以更改/重新分配从类实例化的常量值

基础教程推荐