首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >简单的Getter/Setter注释

简单的Getter/Setter注释
EN

Stack Overflow用户
提问于 2009-06-23 03:26:53
回答 13查看 75.6K关注 0票数 134

您使用什么约定来注释getter和setter?这是我很长一段时间想知道的事情,例如:

/**
 * (1a) what do you put here?
 * @param salary (1b) what do you put here?
 */
public void setSalary(float salary);

/*
 * (2a) what do you put here?
 * @return (2b)
 */
public float getSalary();

我总是发现我为1a/b和2a/b写的东西几乎是一样的,比如1a)设置员工的工资,1b)员工的工资。这看起来太多余了。现在我可以看到对于更复杂的东西,你可能会在(a)部分写更多,以给出上下文,但对于大多数getter/setter来说,措辞几乎是完全相同的。

我只是好奇,对于简单的getter/setter,是否可以只填写(a)部分或(b)部分。

你认为如何?

EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2009-06-22 19:31:21

我通常只填充setter的param部分,以及getter的@return部分:

/**
 * 
 * @param salary salary to set (in cents)
 */
public void setSalary(float salary);

/**
 * @return current salary (in cents, may be imaginary for weird employees)
 */
public float getSalary();

这样一来,javadoc检查工具(比如Eclipse的警告)就会干净地显示出来,而且不会出现重复。

票数 93
EN

Stack Overflow用户

发布于 2009-06-22 20:00:49

完全没有意义--没有这些乱七八糟的代码,你会过得更好:

/**
 * Sets the foo.
 * 
 * @param foo the foo to set
 */
public void setFoo(float foo);

非常有用,如果有保证的话:

/**
 * Foo is the adjustment factor used in the Bar-calculation. It has a default
 * value depending on the Baz type, but can be adjusted on a per-case base.
 * 
 * @param foo must be greater than 0 and not greater than MAX_FOO.
 */
public void setFoo(float foo);

特别是对属性实际含义的解释在领域模型中可能是至关重要的。每当我看到满是只有投资银行家、生物化学家或量子物理学家才能理解的、名称晦涩难懂的属性时,评论解释说setGobbledygook()方法“设置了令人费解的东西”,我就想勒死某人。

票数 186
EN

Stack Overflow用户

发布于 2009-06-22 19:29:27

一般没什么,如果我能帮上忙的话。Getter和setter应该是不言自明的。

我知道这听起来像是不回答,但我试着用我的时间来评论需要解释的事情。

票数 38
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1028967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档