Getter和Setter应该做什么和不应该做什么 [英] What Getters and Setters should and shouldn't do

查看:84
本文介绍了Getter和Setter应该做什么和不应该做什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
公约问题:何时您使用Getter/Setter函数而不是使用Property

Possible Duplicate:
Convention question: When do you use a Getter/Setter function rather than using a Property

我最近在Getters和Setters上遇到了许多不同的看法,所以我认为我应该将其纳入自己的问题中.

I've run into a lot of differing opinions on Getters and Setters lately, so I figured I should make it into it's own question.

A 上一个问题立即发表评论(后来删除),指出二传手不会有任何副作用,而SetProperty方法将是一个更好的选择.

A previous question of mine received an immediate comment (later deleted) that stated setters shouldn't have any side effects, and a SetProperty method would be a better choice.

实际上,这似乎是 Microsoft的意见.但是,当设置表单的WidthHeight属性时,它们的属性通常会引发事件,例如Resized. OwenP 还指出您不应该让属性引发异常,属性不应该有副作用,顺序不重要,并且属性应该相对较快地返回."

Indeed, this seems to be Microsoft's opinion as well. However, their properties often raise events, such as Resized when a form's Width or Height property is set. OwenP also states "you shouldn't let a property throw exceptions, properties shouldn't have side effects, order shouldn't matter, and properties should return relatively quickly."

Michael Stum 指出,例外情况应在设置器中验证数据时被抛出.如果您的设置者没有抛出异常,那么您将如何有效地验证数据,如

Yet Michael Stum states that exceptions should be thrown while validating data within a setter. If your setter doesn't throw an exception, how could you effectively validate data, as so many of the answers to this question suggest?

什么时候需要发起活动,就像Microsoft Control的几乎所有活动一样?那么,您是否会受到订阅该活动的人的摆布呢?如果他们的处理程序执行了大量信息,或者本身抛出错误,那么您的安装程序会如何处理?

What about when you need to raise an event, like nearly all of Microsoft's Control's do? Aren't you then at the mercy of whomever subscribed to your event? If their handler performs a massive amount of information, or throws an error itself, what happens to your setter?

最后,如何在吸气器中延迟加载?这也可能违反先前的准则.

Finally, what about lazy loading within the getter? This too could violate the previous guidelines.

什么是可接受的放置在getter或setter中的内容,什么应该仅保留在访问器方法中?

What is acceptable to place in a getter or setter, and what should be kept in only accessor methods?

从另一个文章在MSDN中:

From another article in the MSDN:

getset方法通常与其他方法没有什么不同.它们可以执行任何程序逻辑,引发异常,被重写以及使用编程语言允许的任何修饰符进行声明.但是请注意,属性也可以是静态的.如果属性是静态的,则getset方法可以执行的操作受到限制.有关详细信息,请参见您的编程语言参考.

The get and set methods are generally no different from other methods. They can perform any program logic, throw exceptions, be overridden, and be declared with any modifiers allowed by the programming language. Note, however, that properties can also be static. If a property is static, there are limitations on what the get and set methods can do. See your programming language reference for details.

推荐答案

我的观点:

  1. 如果期望使用setter或getter昂贵,请不要将其设置为属性,而应将其设置为方法.

  1. If a setter or getter is expected to be expensive, don't make it a property, make it a method.

如果设置属性会由于更改而触发事件,则可以.您还如何允许侦听器收到更改通知?但是,您可能希望提供一个BeginInit/EndInit对以抑制事件,直到进行所有更改为止.通常,事件处理程序有责任及时返回,但是如果您真的不信任它返回,那么您可能希望在另一个线程中发信号通知该事件.

If setting a property triggers events due to changes, this is fine. How else would you allow listeners to be notified of changes? However, you may want to offer a BeginInit/EndInit pair to suppress events until all changes are made. Normally, it is the responsibility of the event handler to return promptly, but if you really can't trust it to do so, then you may wish to signal the event in another thread.

如果设置属性会在无效值上引发异常,那也可以.当值完全错误时,这是​​发出问题信号的合理方法.在其他情况下,您可以设置一堆属性,然后调用使用它们进行某些操作(例如建立连接)的方法.这将允许推迟验证和错误处理,直到使用这些属性为止,因此这些属性将不需要抛出任何内容.

If setting a property throws exceptions on invalid values, it's also fine. This is a reasonable way to signal the problem when the value is completely wrong. In other cases, you set a bunch of properties and then call a method that uses them to do something, such as make a connection. This would allow holding off validation and error-handling until the properties are used, so the properties would not need to throw anything.

访问属性可能会有副作用,只要它们不是意外的和无关紧要的.这意味着在getter中进行JIT实例化就可以了.同样,只要进行更改即可为实例设置一个脏标志,因为它设置了相关属性,例如为相同的值设置了不同的格式.

Accessing a property may have side-effects so long as they aren't unexpected and don't matter. This means a JIT instantiation in a getter is fine. Likewise, setting a dirty flag for the instance whenever a change is made is just fine, as it setting a related property, such as a different format for the same value.

如果做某事(而不只是访问一个值),它应该是一种方法.方法是动词,因此创建连接将由OpenConnection()方法完成,而不是由Connection属性完成. Connection属性将用于检索使用中的连接,或将实例绑定到另一个连接.

If it does something as opposed to just accessing a value, it should be a method. Method are verbs, so creating a connection would be done by the OpenConnection() method, not a Connection property. A Connection property would be used to retrieve the connection in use, or to bind the instance to another connection.

编辑-添加了5,更改了2和3

这篇关于Getter和Setter应该做什么和不应该做什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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