如果我有一個包內的類說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已經達到了答案。 –