返回类型不同的吸气剂是否有资格作为吸气剂? [英] Does a getter with a different return type qualify as a getter?

查看:59
本文介绍了返回类型不同的吸气剂是否有资格作为吸气剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为

List<String> names;

如果我有类似的方法

Iterator getNames() { return names.iterator(); }

Iterator getNames() { return names.iterator(); }

从技术上讲,它仍然是一种吸气方法,因为我将其更改为Iterator吗?

Is it technically still a getter method because i changed it to Iterator?

推荐答案

即使JDK允许这样做,也不是一个好主意.

Even though the JDK allows this, its not a good idea to do it.

其中一种是基于方法命名的约定,这些约定依赖于它们.

For one, there are conventions based off of the naming of your methods, that have some dependencies on them.

  1. JavaBean对象使用getter和setter设置bean属性.如果他们不遵守约定,程序将失败.请参见 JavaBean规范,第8.3.1节

JSON对象-JSONObject类在构造函数中使用accessor方法. 请参阅: http://www.json.org /javadoc/org/json/JSONObject.html#JSONObject(java.lang.Object )

JSON objects - the JSONObject class uses the accessor methods in the constructor. See: http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.Object)

如果某些JPA实现具有多个具有相同名称但返回类型不同的getter,则它们将错误地处理您的实体类.

Some JPA implementations will incorrectly process your entity classes if they have multiple getters with the same name but different return types.

我敢肯定会有更多的依赖项,但是即使这些依赖项不适用于您的解决方案,出于可读性考虑,遵循约定也是明智的.您不希望任何其他开发者在将来的某个时间诅咒您违反标准:)

I bet there are more dependencies, but even if these don't apply to your solution, it's still wise to follow the convention for the sake of readability. You don't want any other developer cursing you sometime in the future for breaking the standards :)

这篇关于返回类型不同的吸气剂是否有资格作为吸气剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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