静态导入方法的好用例是什么? [英] What is a good use case for static import of methods?

查看:25
本文介绍了静态导入方法的好用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚收到评论评论说我的方法的静态导入不是一个好主意.静态导入是来自 DA 类的方法,该类主要包含静态方法.所以在业务逻辑中间,我有一个 da 活动,显然似乎属于当前类:

Just got a review comment that my static import of the method was not a good idea. The static import was of a method from a DA class, which has mostly static methods. So in middle of the business logic I had a da activity that apparently seemed to belong to the current class:

import static some.package.DA.*;
class BusinessObject {
  void someMethod() {
    ....
    save(this);
  }
} 

审阅者并不热衷于我更改代码,我也没有,但我确实同意他的看法.给出不静态导入的一个原因是它在定义方法的地方令人困惑,它不在当前类中也不在任何超类中,因此识别其定义需要时间(基于 Web 的审查系统没有可点击的IDE 之类的链接 :-) 我真的不认为这很重要,静态导入仍然很新,很快我们都会习惯于定位它们.

The reviewer was not keen that I change the code and I didn't but I do kind of agree with him. One reason given for not static-importing was it was confusing where the method was defined, it wasn't in the current class and not in any superclass so it too some time to identify its definition (the web based review system does not have clickable links like IDE :-) I don't really think this matters, static-imports are still quite new and soon we will all get used to locating them.

但我同意的另一个原因是,未限定的方法调用似乎属于当前对象,不应跳转上下文.但如果它真的属于,那么扩展那个超类是有意义的.

But the other reason, the one I agree with, is that an unqualified method call seems to belong to current object and should not jump contexts. But if it really did belong, it would make sense to extend that super class.

那么,什么时候对静态导入方法有意义?你什么时候做过?你喜欢不合格电话的样子吗?

So, when does it make sense to static import methods? When have you done it? Did/do you like the way the unqualified calls look?

流行的观点似乎是静态导入方法,如果没有人会将它们混淆为当前类的方法.例如来自 java.lang.Math 和 java.awt.Color 的方法.但是如果 abs 和 getAlpha 没有歧义,我不明白为什么 readEmployee 是.与许多编程选择一样,我认为这也是个人喜好.

The popular opinion seems to be that static-import methods if nobody is going to confuse them as methods of the current class. For example methods from java.lang.Math and java.awt.Color. But if abs and getAlpha are not ambiguous I don't see why readEmployee is. As in lot of programming choices, I think this too is a personal preference thing.

推荐答案

这是来自 Sun 发布该功能时的指南(强调原文):

This is from Sun's guide when they released the feature (emphasis in original):

那么什么时候应该使用静态导入呢?非常谨慎!仅当您想声明常量的本地副本或滥用继承(常量接口反模式)时才使用它....如果您过度使用静态导入功能,它会使您的程序不可读和不可维护,您导入的所有静态成员都会污染其命名空间.你的代码的读者(包括你,在你编写它几个月后)不会知道静态成员来自哪个类.从类中导入所有静态成员对可读性特别有害;如果您只需要一两个成员,请单独导入.

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). ... If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually.

(https://docs.oracle.com/javase/8/docs/technotes/guides/language/static-import.html)

有两部分我想特别指出:

There are two parts I want to call out specifically:

  • 在您想滥用继承"时使用静态导入.在这种情况下,您是否愿意让 BusinessObject extend some.package.DA?如果是这样,静态导入可能是一种更简洁的处理方式.如果您从未梦想过扩展 some.package.DA,那么这可能是对静态导入的不良使用.不要只是为了在打字时保存几个字符而使用它.
  • 导入单个成员.import static some.package.DA.save 而不是 DA.*.这将使查找此导入方法的来源变得更加容易.
  • Use static imports only when you were tempted to "abuse inheritance". In this case, would you have been tempted to have BusinessObject extend some.package.DA? If so, static imports may be a cleaner way of handling this. If you never would have dreamed of extending some.package.DA, then this is probably a poor use of static imports. Don't use it just to save a few characters when typing.
  • Import individual members. Say import static some.package.DA.save instead of DA.*. That will make it much easier to find where this imported method is coming from.

就我个人而言,我非常很少使用这种语言特性,而且几乎总是只使用常量或枚举,从不使用方法.对我来说,这种权衡几乎不值得.

Personally, I have used this language feature very rarely, and almost always only with constants or enums, never with methods. The trade-off, for me, is almost never worth it.

这篇关于静态导入方法的好用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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