Java “存取方法”的定义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/623618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:04:02  来源:igfitidea点击:

What is the definition of "accessor method"?

javadefinitionaccessor

提问by Rytmis

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.

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

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

我知道你们无法为我赢得争论,但我很想知道你们如何定义这个词。:)

采纳答案by coobird

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

通过访问器,我倾向于想到 getter 和 setter。

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.

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

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

例如,该BigDecimal.add方法是否应该被视为访问器?它是一个方法,它将读取add调用该方法的实例的值,然后在添加另一个 的值后返回结果BigIntegeradd实例方法既不是 getter 也不是 setter ,这似乎相当直接。

回答by Don Branson

I've always gone by the first definition. So, generally that applies just to getters and setters. If we go by the second method, then it's a far less useful distinction, since that covers almost all methods.

我一直遵循第一个定义。因此,通常这仅适用于 getter 和 setter。如果我们采用第二种方法,那么它的用处就不大了,因为它几乎涵盖了所有方法。

回答by Drew Noakes

An accessor method does exactly what it says on the tin: accesses some state from the type without side effects (apart from lazy instantiation, perhaps, which is not something that the caller would normally know about).

访问器方法完全按照它所说的去做:从类型中访问一些状态而没有副作用(除了延迟实例化,这可能不是调用者通常会知道的事情)。

private int _age;

public int getAge()
{
    return _age;
}

Methods that modify state are more usefully considered (in my opinion) as mutators.

修改状态的方法被视为(在我看来)更有用的修改器。

回答by John Ellinwood

Besides googling and wikipedia, the Java Language Specificationshows this as an example of an accessor method:

除了谷歌搜索和维基百科,Java 语言规范将其显示为访问器方法的示例:

private static int N;
public static int getN() { return N; }

So, yes, I'd say it just gets the value of a field. The compiler may inline this, converting it to a simple read, so anything more than that probably isn't an accessor.

所以,是的,我会说它只是获取字段的值。编译器可能会内联它,将其转换为简单的读取,因此除此之外的任何东西都可能不是访问器。

回答by akf

It is good to be able to differentiate getters and setters in technical conversation. Accessormethods are partners to modifiermethods. Accessors read the state of an object (getA()), while modifiers write the state (setA(Object)).

能够在技术对话中区分 getter 和 setter 是件好事。 Accessor方法是方法的伙伴modifierAccessors 读取对象的状态 ( getA()),而modifiers 写入状态 ( setA(Object))。

回答by Abdulfatah Ahmad Irro Jr.

Accessor methods?:getRed, getGreen, and getBlue

访问器方法?:getRed、getGreen 和 getBlue

These methods usually access a value.

这些方法通常访问一个值。

Mutator methods?:setRed, setGreen, setBlue

突变方法?:setRed、setGreen、setBlue

A mutator will mutate a value

mutator 会改变一个值

回答by andru

Accessor methods are used to access fields of an object. So getters and setters are both accessor methods. Observer methodis the right term for a method that makes a more general observation about an object, without causing externally observable side effects. A method whose primary purpose is to cause side effects is a mutator method. Therefore, setters are an exampleof a mutator method. For good engineering practice, public setters should be avoided because they make it impossible for a class to enforce invariants on its data: they violate the abstraction barrier that a class should ordinarily enforce.

访问器方法用于访问对象的字段。所以 getter 和 setter 都是访问器方法。观察者方法是对对象进行更一般性观察而不引起外部可观察的副作用的方法的正确术语。主要目的是引起副作用的方法mutator 方法。因此,setter 是mutator 方法的一个例子。对于良好的工程实践,应该避免公共设置器,因为它们使类无法对其数据强制执行不变量:它们违反了类通常应该强制执行的抽象障碍。

回答by Happyblue

A method that provides access (can be 'read access' or'write access') to the internals of an object is an 'accessor method'.

提供对对象内部的访问(可以是“读访问”“写访问”)的方法是“访问器方法”。

The authors here certainly uses it in this manner:

这里的作者当然以这种方式使用它:

  1. http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html

  2. http://c2.com/cgi/wiki?AccessorsAreEvil

  1. http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html

  2. http://c2.com/cgi/wiki?AccessorsAreEvil

I think the term may originate from Common Lisp (doesn't everything?) -- with setf used to modify the value of accessor slots.

我认为这个术语可能起源于 Common Lisp(不是所有的东西吗?)——用 setf 来修改访问器插槽的值。