有关包的使用,以下是我写的生成包和导入包的两个类
一、生成包类
package parent.child;
public class Location
{
public void disp()
{
System.out.println("child子包中的Location类");
}
public Location()
{
disp();
}
}二、导入类
//import parent.child.Location; // (1)
import parent.child.*;// (2) 用*号为什么出错?但Jbuilder2005却可以
public class ParentTest
{
public static void main(String args[])
{
Location loc = new Location();
//parent.child.Location loc = new parent.child.Location();
}
}问题:
当使用(1)中的语句import parent.child.Location导入具体的类时,没有任何问题
当使用(2)中的语句import parent.child.*导入child所有类时,编译不能通过困惑,请问这是什么原因,导入具体类都可以,导入所有类为什么会出错?
一、生成包类
package parent.child;
public class Location
{
public void disp()
{
System.out.println("child子包中的Location类");
}
public Location()
{
disp();
}
}二、导入类
//import parent.child.Location; // (1)
import parent.child.*;// (2) 用*号为什么出错?但Jbuilder2005却可以
public class ParentTest
{
public static void main(String args[])
{
Location loc = new Location();
//parent.child.Location loc = new parent.child.Location();
}
}问题:
当使用(1)中的语句import parent.child.Location导入具体的类时,没有任何问题
当使用(2)中的语句import parent.child.*导入child所有类时,编译不能通过困惑,请问这是什么原因,导入具体类都可以,导入所有类为什么会出错?
没问题,我刚才试过了,楼主是不是文件路径不对,建议使用Eclipse编译!我在Jbuilder2005两种方式都可以正确执行,但在命令行窗口就有下列问题了
当使用(1)中的语句import parent.child.Location导入具体的类时,没有任何问题
当使用(2)中的语句import parent.child.*导入child所有类时,编译不能通过
这是编译器的bug
我也碰到过
生成的Loation.class在当前路径的 parent.child下面,
用导入包import parent.child.Loaction;没有问题
但用导入包import parent.child.*;编译时通不过
错误信息如下:
ParentTest.java:18: cannot access Location
bad class file: .\Location.java
file does not contain class Location
Please remove or make sure it appears in the correct subdirectory of the classpath.
Location loc = new Location();
^
1 error我用Jbuilder2005两种方式都可以通过
但为什么Location.java和ParentTest.java在同一目录时
用导入包import parent.child.Loaction;没有问题
但用导入包import parent.child.*;编译时通不过这个问题改如何解释呢?
学习么!
初学者用JB,实在不是好的选择!!因为有些问题是JB造成的,不是Java问题!我用JB編譯沒有問題,現在是用CMD編譯的時候出了問題,把Loation.java也放在parent.child下面也可正常運行,就是放在當前目錄下面的時候javac ParentTest.java不能編譯通過。
但import parent.child.Location導入具體類卻有可以編譯
TO hl8189(9)你那个在Eclipse中是不存在问题的,你的第一个加包,第二个为什么不加呢?你故意的还是忘记了?建议每个类的前面都要加上个包名,便于管理。我還沒學會用Eclipse,現在基本用CMD,第一個加包,第二個我想從parent.child.*導入類
程序没有问题
如果程序有问题的话,那java中就没有办法用.*导入包了
回答第2个,你可能没把*.java的文件也放入包内,因为只把类文件放入的话是无法用*号来导入所有是這樣的嗎?那麼就是說.class和.java文件必須放在一起才能用import.*了?
直接導入類都可以,謝謝你的提醒
我再測試一下,
我用Jbuilder2006编译通过!没有问题!我用Jbuilder2005也可以通過,沒有問題,
問題在於用命令行CMD編譯時,import.parent.child.Location;可以通過
import.parent.child.*;編譯時就出錯了错误信息如下:
ParentTest.java:18: cannot access Location
bad class file: .\Location.java
file does not contain class Location
Please remove or make sure it appears in the correct subdirectory of the classpath.
Location loc = new Location();
^
1 error
编译完应该时这个结构
/parent/child/Location.java
/parent/child/Location.class
/ParentTeset.java
/ParentTeset.class
Jbuilder整合了开发环境,可以通过
人人为我,我为人人
在CMD下编译要注意路径问题。。
编译完应该时这个结构
/parent/child/Location.java
/parent/child/Location.class
/ParentTeset.java
/ParentTeset.class是的,照这样编译可以正常编译运行TO 88324877(寂寞呆头鱼)
我用cmd试过也和楼主一样的问题,JCreator试了下也有上述问题,估计是BUG
Jbuilder整合了开发环境,可以通过看来还是有人碰到类似的问题TO 88324877(寂寞呆头鱼)
导人*.class并不是个好习惯,你看的懂别人但是别人看的吃力,最好目标明确点导入确定的类,
人人为我,我为人人谢谢提醒,不过我刚学Java,可能为了省事,我看过其他人也用import.*导入类
是不是jdk有问题~看来只能这样认为了,不知道在你们的JDK的环境下会不会抱错呢?
我试过三台电脑都是同样的错误。
TO bush_80_0(西方迷)
命令行话,classpath是一个很烦人classpath 我设了好几个都试过了 . parent.child ,绝对路径也设了
都没用
没有加package应该是在一个默认的包里面呀?