什么是“静态"?“导入"后的修饰符意思? [英] What does the "static" modifier after "import" mean?

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

问题描述

像这样使用时:

import static com.showboy.Myclass;

public class Anotherclass{}

import static com.showboy.Myclassimport com.showboy.Myclass 有什么区别?

推荐答案

参见 文档

静态导入声明是类似于正常导入宣言.哪里正常导入声明从包,允许它们被使用没有包装资质,静态导入声明导入类中的静态成员,允许它们无需上课即可使用资质.

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

那么什么时候应该使用静态导入呢?非常节俭!仅在您需要时使用它否则很想声明本地常量的副本,或滥用继承(常量接口反模式).换句话说,使用它当您需要经常访问来自一两个的静态成员类.如果你过度使用静态导入功能,它可以使您的程序不可读和不可维护,用所有的东西污染它的命名空间您导入的静态成员.的读者你的代码(包括你,几个月在你写完之后)不会知道静态成员来自哪个类从.导入所有静态一个类的成员可以是特别不利于可读性;如果您只需要一两个成员,单独导入它们.用过的适当地,静态导入可以使您的程序更具可读性,通过删除重复的样板类名.

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). In other words, use it when you require frequent access to static members from one or two classes. 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. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

这篇关于什么是“静态"?“导入"后的修饰符意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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