问题
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