简体   繁体   English

Java Getter Setter代码样式

[英]Java Getter Setter Codestyle

Outside of the context of beans, reflection, introspection or any other often referenced nonsense, is there an important reason that Java Getter/Setter are always notated as Type getAttribute() and void setAttribute(Type a) ? 在bean的上下文之外,反射,自省或任何其他经常引用的废话,是否有重要的原因导致Java Getter / Setter始终被标记为Type getAttribute()void setAttribute(Type a)吗?

I read and wrote a lot of C++ code in recent times and when coming back to Java, I suddenly felt the urge to use Type attribute() and void attribute(Type a) as signatures for getters and setters as they somehow feel more comfortable to use all of a sudden. 最近我读和写了很多C ++代码,回到Java时,我突然感到迫切需要使用Type attribute()void attribute(Type a)作为getter和setter的签名,因为它们以某种方式使人感到更舒服突然使用。 It reminds me of functional programming, having the attribute as a method of the object instead of having a method explicitly change or access the attribute. 它使我想起函数式编程,它具有将属性作为对象的方法,而不是具有显式更改或访问属性的方法。

You can use the methods the way you are comfortable with; 您可以使用自己喜欢的方法;

Type attribute() and void attribute(Type a) Type attribute()void attribute(Type a)

The reason it is as you first example 原因是您的第一个示例

Type getAttribute() and void setAttribute(Type a)

is used is to make it obvious what the method is to be used for. “使用”是为了使该方法使用的目的显而易见。 For example and new developer to a project can pick up and understand the flow of code without moving between different classes to see what that method does. 例如,一个项目的新开发人员可以选择并理解代码流,而无需在不同的类之间移动以查看该方法的作用。

Getters & Setters are usually only one line functions. Getters和Setters通常只是一个行函数。 If a function is to do some data manipluation, it with usually use a descriptive name rather have a get or a set. 如果函数要执行某些数据操作,则通常使用描述性名称,而不要使用get或set。

Summary: Getters & Setters are mainly used for entity objects, where no data manipluation should be done, NOT saying that it can't be done. 简介: Getters和Setters主要用于实体对象,在该对象中不应执行任何数据操作,而不是说不能完成。

The shorter style is the one I use. 较短的样式是我使用的样式。 AFAIK Those in low level Java programming tend to use it possibly because it's more like C++, or because it's less like EJB's. AFAIK那些从事低级Java编程的人可能会使用它,因为它更像C ++,或者因为它不像EJB。

The problem with the JavaBean getter/setter style is it assumes an implementation of just setting and getting the variable, however this is not always the case. JavaBean getter / setter样式的问题在于,它假定仅设置和获取变量的实现,但是并非总是如此。

The Java Naming Conventions state that " Methods should be verbs ", which is commonly generalized by the community to " Methods should start with a verb ". Java命名约定规定“ 方法应为动词 ”,社区通常将其概括为“ 方法应以动词开头 ”。 It is a question of consistency. 这是一致性的问题。 You may very well use attribute , but I can guarantee you that people will confuse it. 您可能会很好地使用attribute ,但是我可以保证您会引起人们的混淆。 So if you expect other people to read and change you code, I strongly suggest to go for getAttribute and setAttribute . 因此,如果您希望其他人阅读和更改您的代码,我强烈建议您使用getAttributesetAttribute This argument is supported by Robert C. Martin in his book Clean Code (Section "Method Names"). 罗伯特·C·马丁Robert C. Martin)在他的《 清洁代码》 (“方法名称”部分)中支持此论点。 It explicitly deals with your case. 它明确处理您的情况。

That being said, the Java-API itself violates this rule sometimes (for example with the method size() in Collections). 话虽这么说,Java-API本身有时违反了此规则(例如,使用Collections中的size()方法)。 This is a known problem but shouldn't stop you from doing it better. 这是一个已知的问题,但不应该阻止您做得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM