你怎么称呼一个定义了 getter 和 setter 的变量,没有后备变量? [英] What do you call a variable that has a getter and setter defined, with no backing variable?

查看:62
本文介绍了你怎么称呼一个定义了 getter 和 setter 的变量,没有后备变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用 getter 和 setter 定义变量/属性时,它被称为什么,以便语言不会自动生成支持变量?

What is it called when you define a variable/property with a getter and setter, such that the language does not automatically generate a backing variable?

例如在 Swift 中,我们可以定义一个不创建支持变量的 modalViewController 属性:

E.g. in Swift, we could define a modalViewController property that doesn't create a backing variable:

extension MyViewController {
    var modalViewController: UIViewController? {
        get { return self.presentedViewController }
        set { self.present(newValue, animated: true) }
    }
}

描述 modalViewController 属性的正确术语是什么?

What's the proper term to describe the modalViewController property?

我知道如果它只是可获取的,它将被称为计算属性:

I know that if it's only gettable, it would be called a computed property:

extension Int {
    var isEven: Bool { 
        get { return self % 2 == 0 }
    }
}

但是,我正在寻找一个术语来表示既可设置又可获取.

However, I'm looking for a term for something that is both settable and gettable.

我寻找一个术语的原因是我想提出一个与这些类型的属性相关的问题,并且想使用通用的、非歧义的语言.我认为这将被称为虚拟财产,但它似乎不是 虚拟在 OOP 中有不同的含义.

The reason I'm looking for a term is that I want to ask a question related to these types of properties, and would like to use common, non-ambiguous language. I thought this would be called a virtual property, but it doesn't appear to be the proper name as virtual has a different meaning in OOP.

推荐答案

即使他们也有一个 setter,Swift(至少)调用这些计算属性"(强调):

Even though they have a setter too, Swift (at least) calls these "computed properties" (emphasis added):

除了存储属性之外,类、结构和枚举还可以定义计算属性,它们实际上并不存储值.相反,它们提供了一个 getter 和一个可选的 setter 来间接检索和设置其他属性和值.

In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.

这篇关于你怎么称呼一个定义了 getter 和 setter 的变量,没有后备变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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