What do you call a variable that has a getter and setter defined, with no backing variable?

江枫思渺然 提交于 2020-01-16 12:05:01

问题


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?

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) }
    }
}

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.


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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/59339650/what-do-you-call-a-variable-that-has-a-getter-and-setter-defined-with-no-backin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!