如果我有一个包内的类说mypackage.myclass
,并且想要访问与包的根(不在包内)相同的文件夹中的包的类说anothepackage.anotherclass
我怎么能这样做?从包中访问父文件夹包
示例代码中提出:
// directories are preceeded by a DIR, files are precedded by a CLASS
// any level deeper is preceeded by a [Tab]
DIR mypackage
CLASS myclass
DIR myinnerpackage
CLASS myinnerclass
DIR anotherpackage
CLASS anotheclass
CLASS yetanotherclass
DIR org
DIR apache
DIR commons
DIR exec
CLASS DefaultExecutor
里面的文件MyClass的:
package mypackage;
public class myclass{
// simplest example code I can come up with
org.apache.commons.exec.DefaultExecutor exec = new org.apache.commons.exec.DefaultExecutor();
}
为什么这段代码不适合我?
一般如何才能myclass
访问anotherclass
或yetanotherclass
以及myinnerclass
如何能够使用它们?尽量避免导入,因为我只想使用每个类的某些成员(一些变量具有相同的名称,并且会让事情变得混乱)!提前致谢!
你在问'import'语句吗? –
不,我问如何访问另一个类的父目录中的类。 –
“访问”是什么意思?如果你想在一个不同的包中使用一个类,你可以使用'import'语句(但这是你可能已经知道的基本东西(:)。如果你想获得文件内容(或相关的),那么记住包是只是文件夹和Class.java是文件,如果最后一个是你的情况,那么Eng.Fouad已经达到了答案。 –