简体   繁体   English

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

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

For example while importing classes from IO package in java we use import java.io.* , instead of this why cant we use import java.io as import statement? 例如,在java中从IO包导入类时,我们使用import java.io.* ,而不是为什么我们不能使用import java.io作为import语句? When i use import java.io i get an error stating location: package java . 当我使用import java.io我收到一个错误说明位置: package java Why is this error thrown? 为什么抛出这个错误?

You can only import classes not package. 您只能导入类而不是包。 import java.io.* will import all classes in java.io package 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。

see this page for more information on it 有关它的更多信息, 请参阅此页面

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 package by 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 io and a class named io . 由于约定而不是任何Java限制,您将无法区分子包io和名为io的类。 There is no compile error if you name a class not starting with an uppercase letter. 如果您将一个类命名为不以大写字母开头,则不会出现编译错误。

In Java, you can import classes and packages. 在Java中,您可以导入类和包。 To import a class, you can use the fully qualified name of the class like 要导入类,可以使用类的完全限定名称

import com.pkg.spkg.ClassName; .

The package spkg may contain a lot of classes, and you require all of them in your application. spkg可能包含很多类,您需要在应用程序中使用所有类。 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 因此要导入整个包,我们可以使用通配符*

import com.pkg.spkg.*;

Hope this makes it clear. 希望这说清楚。

import java.io.* will import all the classes from the io package. import java.io.*将导入io包中的所有类。 io is the name of the package and you need to import only classes. io是包的名称,您只需要导入类。

You can alternatively import just the classes you need. 您也可以只导入所需的类。 Eg: import java.io.BufferedInputStream 例如: import java.io.BufferedInputStream

Have a look at java.io docs to find all the classes defined in the java.io package. 查看java.io docs以查找java.io包中定义的所有类。

Also have a look at the tutorial for packages , to understand all about packages in java. 还要看一下教程 ,了解java中的所有包。

Using * on 使用* on

import java.io.*

Will import every classes in the io directory, you won't be able to import a directory. 将导入io目录中的每个类,您将无法导入目录。

The second statement treated io as class, not package. 第二个语句将io视为类,而不是包。 That's why the error. 这就是错误的原因。 You should read it carefully and use Java Naming conventions to properly name your classes. 您应仔细阅读并使用Java命名约定来正确命名您的类。

The correct usage of import statement if you want to use java.io package classes 如果要使用java.io包类,请正确使用import语句

import java.io.*; 

import java.io; means import class io from package java . 表示从包java导入class io but the io class does not exist. 但是io类不存在。 java.io is a package, not a class. java.io是一个包,而不是一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM