java 如何在java程序中导入用户定义的包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4458330/
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-10-30 06:29:23  来源:igfitidea点击:

how to import user defined packages in java program?

java

提问by vinoth kumar

some.java
import A.A1.*;

Directory structure
  A
  |_A2
  |  |_some.java
  |_A1
     |_someother files and java files

but import statement didn't work. What is the reason?

但导入语句不起作用。是什么原因?

回答by jrharshath

I hope Ais not your current directory.

我希望A不是你当前的目录。

Perhaps you already know that packages work with directory heirarchies.

也许您已经知道包与目录层次结构一起使用。

<curr-dir>
 |-A
 | |-A1
 | | |- Class1.class
 | | `- Class2.class
 | `-A2
 |   |-Class3.class
 |   `-Class4.class
 `-<other dirs>

Now if <curr-dir>is in your classpath then import A.A1.*will import Class1 and Class2.

现在,如果<curr-dir>在您的类路径中,import A.A1.*则将导入 Class1 和 Class2。

Hope that is detailed enough :)

希望足够详细:)

回答by Barbarrosa

Any class in a given package must have a package statement, and must be declared public (in most cases) to be read outside the package:

给定包中的任何类都必须有一个包语句,并且必须声明为 public(在大多数情况下)才能在包外读取:

package A.A1;
public class ClassName{}

回答by Asha

package A.A1;

包 A.A1;

import A.A2.*;

进口A.A2.*;

public class ex2{}

公开课 ex2{}

回答by Nullpointer

Dude *(star operator) does not work for user created packages! you need to import each class individually..

Dude *(star operator) 不适用于用户创建的包!您需要单独导入每个类..