计算属性(仅限可获取的)与存储属性的优势 [英] Advantage of computed properties (gettable ones only) vs. stored properties

查看:41
本文介绍了计算属性(仅限可获取的)与存储属性的优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一下我是否正确理解了以下概念.

I would like to clarify if I understand the following concept correctly.

假设我的目标是将 String "Good Morning, Mike" 存储到变量 var sayGoodMorningToUser 中.

Assume that my goal is to store the String "Good morning, Mike" into the variable var sayGoodMorningToUser.

String 由两个变量组成,即

The String is composed of two variables, namely

var greeting = "Good morning, "
var username = "Mike"

换言之,如果我使用存储属性与计算属性有什么区别:

What difference does it make if I use stored properties vs. computed properties, in other words:

var sayGoodMorningToUserStored = greeting + username

对比

var sayGoodMorningToUserComputed:String {
     return greeting + username
}

我看到这两种方法的唯一区别是任何人都可以轻松直接地更改 sayGoodMorningToUserStored 的值,例如通过写作

The only difference I see between these two approaches is that anyone could change the value of sayGoodMorningToUserStored easily and directly, e.g. by writing

var sayGoodMorningToUserStored = "myNewChangedValue"

而变量 sayGoodMorningToUserComputed 不能直接修改,因为它不能简单地设置为一个新的字符串值:

whereas the variable sayGoodMorningToUserComputed cannot be modified directly, because it cannot simply be set it to a new String value:

var sayGoodMorningToUserComputed = "Hallo" //this would cause an error 

否则我无法理解为什么人们计算变量而不是简单地编写

Otherwise I cannot understand why people compute the variable instead of simply writing

var sayGoodMorningToUserStored = greeting + username.

谁能解释一下我是否理解正确?或者计算变量与存储变量相比还有其他优势吗?

Can anyone explain if I understood it correctly? Or are there also other advantages of computed variables vs. stored ones?

我想将我的问题仅限于可获取的变量,因为在这里讨论可设置的变量会超出范围.

I would like to limit my question to gettable variables only, because discussing settable variables here would go beyond the scope.

推荐答案

计算属性

var sayGoodMorningToUserComputed: String {
     return greeting + username
}

sayGoodMorningToUserComputed 就像一个函数.如果对 greetingusername 进行了更改,则 sayGoodMorningToUserComputed 将返回最新结果,该结果将是当前值.

sayGoodMorningToUserComputed acts just like a function. If a change has been made to greeting or username, then sayGoodMorningToUserComputed will return an up-to-date result that will be the concatenation of the current values.

如果您想确保返回值是根据其依赖项的最新值(greetingusername)计算出来的,您可能需要使用它.

You would want to use this if you want to ensure your returned value is computed off the latest values of its dependencies (greeting and username).

如果两个依赖都是final,那么编译器很可能会把这个计算属性优化成一个存储属性,因为它知道依赖是不能改变的

In the case that both dependencies are final, then it's very likely that the compiler would optimise this computed property into a stored property, because it knows the dependencies can't change

var sayGoodMorningToUserStored = greeting + username

sayGoodMorningToUserStored 只是一个变量,没有什么特别的.但是,只要初始化包含范围,它就只设置一次.它被计算一次,存储并保持不变,直到被外部源覆盖.因此,如果 greetingusername 更改,则不会对 sayGoodMorningToUserStored 产生影响,因为它是根据旧值计算并存储的.

sayGoodMorningToUserStored is just a variable, with nothing special going on. However, it's only set once, whenever the containing scope is initialized. It's computed once, stored and remains constant until it is overwritten by an external source. As such, if greeting or username changes, there will be no effect on sayGoodMorningToUserStored, because it's been computed from the old values, and stored.

如果您想通过缓存依赖项不变的计算结果来提高性能,您可能需要使用它.

You would want to use this if you want to improve performance by caching the result of a computation whose dependencies are constant.

这篇关于计算属性(仅限可获取的)与存储属性的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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