这些陈述是什么意思?推荐这种风格吗?
最近我发现了一个陈述:
import static java.lang.System.out;
import static java.lang.System.exit;
我在一些教程中读到了这些陈述。这些说法可以吗?
如果这些语句没问题,它们的含义是什么?在编写代码时是否应该定期使用它们?
Recently i cam across a statements :
import static java.lang.System.out;
import static java.lang.System.exit;
I read these statements in some tutorial. Are these statements O.K ?
If the statements are alright what do they mean and should they be used regularly while writing code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它们被称为静态导入。其效果是允许您在程序中使用名称
out
和exit
,就好像它们是在当前作用域中定义的一样;因此您可以编写exit(0)
而不是System.exit(0)
。现在,他们是个好主意吗?有时,如果谨慎使用,它们是减少混乱的好方法。但大多数时候,它们实际上只是让你的代码更难理解。读者会问“这个
out
在哪里定义的?”和“exit()
从哪里来?”一般来说,您应该避免它们。但是,如果您正在编写一个专门处理
SomeReallyLongName
对象的类,并且SomeReallyLongName
定义了一堆FINAL_CONSTANTS
,那么使用静态导入来导入它们将会节省大量的打字和大量的混乱,并且这些常量的来源将非常清楚。They are called static imports. The effect is to allow you to use the names
out
andexit
in your program as if they were defined in the current scope; so you can writeexit(0)
instead ofSystem.exit(0)
.Now, are they a good idea? Sometimes, when used sparingly, they are a good way to reduce clutter. But most of the time, they actually just make your code harder to understand. The reader will ask "Where is this
out
defined?" and "Where doesexit()
come from?" In general, you should avoid them.But if you're writing a class that's all about processing
SomeReallyLongName
objects, andSomeReallyLongName
defines a bunch ofFINAL_CONSTANTS
, importing them with static imports will save a lot of typing and a lot of clutter, and it will be pretty clear where those constants are coming from.它们是静态导入。它允许您执行诸如
exit(0)
之类的操作,而不是System.exit(0)
。我不建议对众所周知的 Java 类使用此方法,因为它可能会让某些人感到困惑。但有时它对于像 Guava 这样的实用程序类很有用。
非常冗长,但您可以通过静态导入使其更易于阅读:
filter(list, SomeClass.class)
您应该与您的团队核实,看看他们的代码指南是什么,并尽量保持一致。
They are static imports. It allows you to do something like
exit(0)
instead ofSystem.exit(0)
.I do not recommend this for well known Java classes because it can be confusing to some. But sometimes it is useful for utility classes like Guava.
is very verbose but you can make it easier to read with static imports:
filter(list, SomeClass.class)
You should check with your team to see what they code guidelines are and try to be consistent.
是的,完全没问题。
这称为
静态导入
。这允许在类中定义为static
的成员,并且使用
public
时无需指定定义该字段的类。此功能在
J2SE 5.0
中定义。例如:
我认为使用静态导入可能不是一个好主意,因为它会使您的代码难以阅读。
Yes,it is perfectly alright .
This is known as
static import
.This allows members defined in class asstatic
andpublic
to be used without specifying the class in which the field is defined.This feature was defined in
J2SE 5.0
.For example :
I think it may not be a good idea to use static imports as it'll make your code hard to read.
是的,这些语句被称为静态导入并且完全有效。查看有关静态导入的 javase 指南< /a> 了解更多信息。
关于使用,指南指出:
Yes, these statements are referred to as
static imports
and are perfectly valid. Take a look at the javase guide on static imports for more information.With respect to usage, the guide states:
静态导入是 Java 1.5 中添加的新功能< /a>
如果您希望轻松访问
out
和exit
以便可以直接将它们调用为,那么您的示例没有任何问题。 >out.println()
例如。它在语法上和风格方面都没有任何错误,尽管有些人可能会认为它“令人困惑”并且很难弄清楚out
来自哪里,但任何现代 IDE 都可以帮助他们弄清楚这一点。Static imports are a new feature added in Java 1.5
There is nothing wrong with your example if you want easy access to
out
andexit
so that you can call them directly asout.println()
for example. There is nothing syntactically incorrect about it nor from a style aspect though some may argue it is "confusing" and hard to figure out whereout
came from, but any modern IDE can help them figure that out.这些是静态导入概念。它们就像简单的导入,但具有不同类型的概念。在这里,您导入一个函数 exit() 和一个字段 out ,并且两者都是静态的它们对应的类(如果这里的 Systen 都是它们的类)。在此之后,您可以简单地编写 out.println(),而不是编写 System.out.println() 。同样代替System.exit(),可以写exit()。
These are static import concept.These are like simple imports but having different type of concept.See here you import one function exit() and one field out and both are static in their corresponding classes(in case both here Systen is their class).After this instead of writing System.out.println() you can simply write out.println().Similarly instead of System.exit(),you can write exit().