Java导入包(包装在上面的工作目录上) [英] Java Import Package (to package above present work directory)
问题描述
如何在Java中导入包(到当前工作目录以上的包)?
how do I import package (to package above present work directory) in Java?
这里是目录结构:
Coba.java
import halo.*;
public class Coba
{
public static void main(String args[])
{
Orang org = new Orang();
System.out.println(org.a);
}
}
Orang.java
package halo;
// I can't import kabar.*; since it's above present work directory
public class Orang
{
public int a;
public Orang()
{
this.a = 1;
}
public void haha()
{
/*
i want to:
Tes t = new Tes();
System.out.println(t.b);
*/
}
}
Tes。 java
package kabar;
public class Tes
{
public int b;
public Tes()
{
this.b = 2;
}
}
问题:
如何通过从类Orang导入类kabar.Tes来访问类Tes中的变量b?
How do I access variable b in class Tes by importing class kabar.Tes from class Orang?
如果我写的话
import kabar.Tes;
它不起作用,因为类Orang在当前工作目录之上。
in class Orang. It doesn't work because class Orang is above present work directory.
非常感谢。
BTW ,我不使用Netbeans或Eclipse。我想知道它的基本工作原理,所以我只使用简单的文本编辑器。
BTW, I don't use Netbeans or Eclipse. I want to know the basic how it works, so I just use simple text editor.
推荐答案
David,
目录的位置无关紧要。重要的是包裹。编译/运行程序以引用这些额外目录时,可以将多个目录添加到类路径中。
David, The location of the directories doesn't matter. It's the packages that matter. You can add multiple directories to your classpath when you compile/run the program to refer to these extra directories.
这篇关于Java导入包(包装在上面的工作目录上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!