Java“用户”类命名最佳实践?
我即将为我的项目创建一个 User 类。对于创建这样一个通用名称的类是否是不好的做法有什么意见吗?我可以用我的项目特有的前缀来补充它。
I'm about to create a User class for my project. Any opinions as to whether it is bad practice to create such a commonly-named class? I could complement it with a prefix specific to my project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是 包 的用途。
This is what packages are for.
我建议您在您的 User 类中明确添加应用程序特定术语。 “用户”太常见和模糊了。您可能很容易最终使用具有“用户”类或接口的 API。尽管最新的 IDE 使重构代码变得相对容易,但如果您有一个特定于应用程序的“User”类,它会更干净、更容易。
I would suggest you to definitely prefix an application specific term to your User class. "User" is way too common and vague. You might easily end up using API which has "User" class or Interface. Though latest IDEs make it relatively easy to refactor your code, it would be cleaner and easier if you have a app specific "User" class.
使用通用名称并不是一个坏习惯,除非您将名称描述为类的功能。请记住创建可以更具体地定义您的类的适当包。
Using common names is not a bad practice, until and unless you describe the name as that to the functionality of the class. keep in mind to create proper packages that can define your class more specifically.
为了清楚起见,我总是尝试使用通用名称,例如 User。只要确保名称取自问题领域和现有项目词汇即可。警惕歧义,如果出现歧义,请更改类名。具有自动重构支持的现代 IDE 使这一切变得简单。
I always try to use common names like User for clarity. Just be sure the names are drawn from the problem domain and existing project vocabulary. Be alert for ambiguities, and if one arise, change the class name. Modern IDEs with automated refactoring support make that easy.
您应该使用 Java
package
来避免名称冲突。使用通用名称没有什么问题,只要确保将其放在自己的包中即可。例如,您可以具有以下结构:然后,您可以使用以下内容开始文件“User.java”:
当您导入它时,您将使用:
You should use Java
package
s to avoid name clashes. There is nothing wrong with using a common name, just be sure to put it in its own package. For example, you could have the following structure:You then begin the file "User.java" with:
When you import it, you would use:
该名称不太可能被任何公共 API 使用,因此应用程序可以使用它。
This name is unlikely to be used by any public API, so it's ok for an app to use it.