“访问者方法”的定义是什么? [英] What is the definition of "accessor method"?

查看:155
本文介绍了“访问者方法”的定义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在讨论访问者这个词的用法(上下文是Java编程)。我倾向于认为访问器隐含地是属性访问器 - 也就是说,该术语暗示它或多或少地提供对对象内部状态的直接访问。另一方坚持认为任何以任何方式触及对象状态的方法都是一个访问者。

I've been having an argument about the usage of the word "accessor" (the context is Java programming). I tend to think of accessors as implicitly being "property accessors" -- that is, the term implies that it's more or less there to provide direct access to the object's internal state. The other party insists that any method that touches the object's state in any way is an accessor.

我知道你们不能为我赢得这个论点,但是我我很想知道如何定义这个术语。 :)

I know you guys can't win the argument for me, but I'm curious to know how you would define the term. :)

推荐答案

通过访问者,我倾向于考虑getter和setter。

By accessors, I tend to think of getters and setters.

通过坚持所有触及对象内部状态的方法都是访问器,似乎实际使用对象状态的任何实例方法都是一个访问器,而这似乎不对。什么样的实例方法不会使用对象的状态?换句话说,一个不以某种方式使用对象状态的实例方法不应该是一个开头的实例方法 - 它应该是一个类方法

By insisting that all methods that touch the object's internal state are accessors, it seems that any instance method that actually uses the state of the object would be an accessor, and that just doesn't seem right. What kind of instance method won't use the state of the object? In other words, an instance method that doesn't use the object's state in some way shouldn't be an instance method to begin with -- it should be a class method.

例如, BigDecimal.add 方法被视为访问者?这是一个方法,它将读取调用 add 方法的实例的值,然后在添加另一个 BigInteger的值后返回结果。似乎很简单, add 实例方法不是getter也不是setter。

For example, should the BigDecimal.add method be considered an accessor? It is a method that will read the value of the instance that the add method was called on, then return the result after adding the value of another BigInteger. It seems fairly straight forward that the add instance method is not a getter nor a setter.

这篇关于“访问者方法”的定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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