为什么我们在从java包中导入类时使用*?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23489568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:22:01  来源:igfitidea点击:

Why do we use * while importing classes from packages in java?

java

提问by Elysium

For example while importing classes from IO package in java we use import java.io.*, instead of this why cant we use import java.ioas import statement? When i use import java.ioi get an error stating location: package java. Why is this error thrown?

例如,在java中从IO包导入类时,我们使用import java.io.*,而不是这个,为什么我们不能import java.io用作import语句?当我使用时,import java.io我收到一个错误说明 location: package java。为什么会抛出这个错误?

采纳答案by niiraj874u

You can only import classes not package. import java.io.*will import all classes in java.iopackage

您只能导入类而不能打包。import java.io.*将导入java.io包中的所有类

To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.

要导入特定包中包含的所有类型,请使用带有星号 ( *) 通配符的 import 语句。

Now you can refer to any class or interface in the package by its simple name.

现在您可以通过简单的名称来引用包中的任何类或接口。

Note: Another, less common form of import allows you to import the public nested classes of an enclosing class. For example, if the graphics.Rectangle class contained useful nested classes, such as Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its nested classes by using the following two statements.

注意:另一种不太常见的导入形式允许您导入封闭类的公共嵌套类。例如,如果 graphics.Rectangle 类包含有用的嵌套类,例如 Rectangle.DoubleWide 和 Rectangle.Square,则可以使用以下两个语句导入 Rectangle 及其嵌套类。

import graphics.Rectangle;
import graphics.Rectangle.*;

Be aware that the second import statement will not import Rectangle.

请注意,第二个 import 语句不会导入 Rectangle。

seethis page for more information on it

请参阅此页面以了解有关它的更多信息

回答by anirudh

import java.io.*will import all the classes from the io package. iois the name of the package and you need to import only classes.

import java.io.*将从 io 包中导入所有类。io是包的名称,您只需要导入类。

You can alternatively import just the classes you need. Eg: import java.io.BufferedInputStream

您也可以只导入您需要的类。例如:import java.io.BufferedInputStream

Have a look at java.iodocs to find all the classes defined in the java.iopackage.

查看java.io文档以查找java.io包中定义的所有类。

Also have a look at the tutorial for packages, to understand all about packages in java.

还可以查看包教程,以了解有关 java 中包的所有信息。

回答by Tifa

Using * on

使用 * on

import java.io.*

Will import every classes in the io directory, you won't be able to import a directory.

将导入 io 目录中的每个类,您将无法导入目录。

回答by Roman C

The second statement treated ioas class, not package. That's why the error. You should read it carefully and use Java Naming conventions to properly name your classes.

第二个语句被io视为类,而不是包。这就是错误的原因。您应该仔细阅读它并使用 Java 命名约定来正确命名您的类。

The correct usage of import statement if you want to use java.iopackage classes

如果要使用java.io包类,import 语句的正确用法

import java.io.*; 

回答by Harmlezz

It's just a matter of the syntax. If you look how to import a specific class:

这只是语法问题。如果您查看如何导入特定类:

import java.util.List;

it seams consistent to express import everything from some packageby using the asterisk *like in so many other environments: pattern matching, Ant, etc.

它接缝一致表示进口一切从一些包用星号*在许多其他环境中,如:pattern matchingAnt等等。

import java.util.*;

The asterisk has a history to match "everything".

星号具有匹配“一切”的历史。

And due to conventions and not any Java restrictions, you would not be able to distinguish between the sub-package ioand a class named io. There is no compile error if you name a class not starting with an uppercase letter.

并且由于约定而不是任何 Java 限制,您将无法区分子包io和名为io. 如果您命名的类不是以大写字母开头,则不会出现编译错误。

回答by Kris

In Java, you can import classes and packages. To import a class, you can use the fully qualified name of the class like

在 Java 中,您可以导入类和包。要导入类,您可以使用类的完全限定名称,例如

import com.pkg.spkg.ClassName;.

import com.pkg.spkg.ClassName;.

The package spkgmay contain a lot of classes, and you require all of them in your application. In such a case the good idea would be to import all the classes or package itself. So to import the whole package we can use the wildcard *like

该包spkg可能包含很多类,您需要在应用程序中使用所有这些类。在这种情况下,最好的办法是导入所有类或包本身。所以要导入整个包,我们可以使用通配符,*

import com.pkg.spkg.*;

Hope this makes it clear.

希望这能说清楚。

回答by Xing Fei

import java.io;means import class iofrom package java. but the ioclass does not exist. java.io is a package, not a class.

import java.io;表示class io从包导入java。但io该类不存在。java.io 是一个包,而不是一个类。