在Java中的Import语句中使用* [英] Use of * in Import Statement in Java

查看:117
本文介绍了在Java中的Import语句中使用*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用Java解释我的import语句。有些导入有*后缀,有些则没有。这两者有什么区别?在import语句中使用*是否会导入所有类?

Can someone explain me the import statements in Java. Some import has * suffixed to it and some doesn't. What is the difference between these two? Does the use of * in the import statement imports all the classes?

请参阅此处 import

这里他们说虽然导入语句似乎是嵌套的,但并非如此。
有人可以详细解释一下吗?

Here they have said that though the import statements seems nested they are not so. Can someone explain in detail?

推荐答案

使用 * 被认为是一种不好的做法。它用于导入该包中的所有文件。更正确的方法是列出您需要的每个类,特别是在您在IDE之外进行代码审查并需要知道您正在使用的类的哪个版本的情况中。从本质上讲,它会在开发团队中产生懒惰。

The use of * is considered a bad practice. It is used to import all files within that package. The more correct way of doing this is listing off each of the classes that you need, specifically in a scenario where you are doing a code review outside of an IDE and need to know which version of the class you are using. Essentially it breeds laziness in the development team.

评论

对于那些认为这不是坏做法的人正如我所说。你怎么能说这是一个好习惯?

For those arguing that this is not a "bad" practice as I have stated. How can you possibly say that this is a good practice?

import java.util.*;  
import java.io.*;

即使编译器忽略了 * 除了您导入的 List 之外,这对于将来查看代码的人有何帮助?我认为这里有很多人忘记你是在为人而不是计算机编写代码。当Java消失并且您正在使用SuperAwesomeLanguage时,您怎么可能转换此代码?如果您对java有零知识,请将此转换为您的新语言:

Even if the compiler ignores everything under the * except the List that you have imported, how does this possibly help someone looking at the code in the future? I think a good deal of people here forget that you are writing code for humans and not the computer. Further how could you possibly convert this code when Java goes away and you are using SuperAwesomeLanguage? Given the following example please convert this to your new language when you have ZERO knowledge of java:

public class Foo  
{
    private List list;
}

列表 in IO ?是 io 甚至是必需的?问题是你不知道。因此,通过明确,您可以指导未来的开发人员,了解所需的类。

Is List in io? is io even required? The issue is you don't know. So by being explicit you can guide future developers as to what classes are required.

这篇关于在Java中的Import语句中使用*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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