这些陈述是什么意思?推荐这种风格吗?

发布于 2024-12-04 14:07:58 字数 203 浏览 0 评论 0原文

最近我发现了一个陈述:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

是你 2024-12-11 14:07:58

它们被称为静态导入。其效果是允许您在程序中使用名称 outexit,就好像它们是在当前作用域中定义的一样;因此您可以编写 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 and exit in your program as if they were defined in the current scope; so you can write exit(0) instead of System.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 does exit() come from?" In general, you should avoid them.

But if you're writing a class that's all about processing SomeReallyLongName objects, and SomeReallyLongName defines a bunch of FINAL_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.

毁虫ゝ 2024-12-11 14:07:58

它们是静态导入。它允许您执行诸如 exit(0) 之类的操作,而不是 System.exit(0)

我不建议对众所周知的 Java 类使用此方法,因为它可能会让某些人感到困惑。但有时它对于像 Guava 这样的实用程序类很有用。

Iterables.filter(list, SomeClass.class)

非常冗长,但您可以通过静态导入使其更易于阅读: filter(list, SomeClass.class)

您应该与您的团队核实,看看他们的代码指南是什么,并尽量保持一致。

They are static imports. It allows you to do something like exit(0) instead of System.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.

Iterables.filter(list, SomeClass.class)

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.

伪装你 2024-12-11 14:07:58

是的,完全没问题。

这称为静态导入。这允许在类中定义为static 的成员,并且
使用 public 时无需指定定义该字段的类。
此功能在 J2SE 5.0 中定义。
例如:

import static java.lang.Math.*;
import static java.lang.System.out; 
// in main
out.print( max(100,200) ); // prints 200.You didn't have to use Math.max(.,.) 

我认为使用静态导入可能不是一个好主意,因为它会使您的代码难以阅读。

Yes,it is perfectly alright .

This is known as static import.This allows members defined in class as static and
public to be used without specifying the class in which the field is defined.
This feature was defined in J2SE 5.0.
For example :

import static java.lang.Math.*;
import static java.lang.System.out; 
// in main
out.print( max(100,200) ); // prints 200.You didn't have to use Math.max(.,.) 

I think it may not be a good idea to use static imports as it'll make your code hard to read.

会发光的星星闪亮亮i 2024-12-11 14:07:58

是的,这些语句被称为静态导入并且完全有效。查看有关静态导入的 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:

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.

定格我的天空 2024-12-11 14:07:58

静态导入是 Java 1.5 中添加的新功能< /a>

静态导入构造允许对静态成员进行非限定访问,而无需从包含静态成员的类型继承。相反,程序会单独导入成员

如果您希望轻松访问 outexit 以便可以直接将它们调用为 ,那么您的示例没有任何问题。 >out.println() 例如。它在语法上和风格方面都没有任何错误,尽管有些人可能会认为它“令人困惑”并且很难弄清楚 out 来自哪里,但任何现代 IDE 都可以帮助他们弄清楚这一点。

Static imports are a new feature added in Java 1.5

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually

There is nothing wrong with your example if you want easy access to out and exit so that you can call them directly as out.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 where out came from, but any modern IDE can help them figure that out.

肤浅与狂妄 2024-12-11 14:07:58

这些是静态导入概念。它们就像简单的导入,但具有不同类型的概念。在这里,您导入一个函数 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文