关于package包的问题,请高手指教

flowerknight 2002-11-11 10:08:29
我在g:盘java目录下建立package目录作为自定义包目录。即g:\java\package作为存储包的目录。
然后定义Item类和Storefront类放在包中代码如下:
Item类:
package comm.perfect.ecommerce;

import java.util.*;

public class Item implements Comparable{
private String id;
private String name;
private double retail;
private int quantity;
private double price;

Item(String idIn,String nameIn,String retailIn,String quanIn)
{
id=idIn;
name=nameIn;
retail=Double.parseDouble(retailIn);
quantity=Integer.parseInt(quanIn);

if(quantity>400)
{
price=retail*.5D;
}
else if(quantity>200)
{
price=retail*.6D;
}
else
{
price=retail*.7D;
}
price=Math.floor(price*100+.5)/100;
}

public int compareTo(Object obj)
{
Item temp=(Item)obj;
if(this.price<temp.price)
{
return 1;
}
else if(this.price>temp.price)
{
return -1;
}
return 0;
}

public String getId()
{
return id;
}

public String getName()
{
return name;
}

public double getRetail()
{
return retail;
}

public int getQuantity()
{
return quantity;
}

public double getPrice()
{
return price;
}
}


Storefront类 :
package comm.perfect.ecommerce;

import java.util.*;

public class Storefront{
private LinkedList catalog=new LinkedList();

public void addItem(String id,String name,String price,String quant)
{
Item it=new Item(id,name,price,quant);
catalog.add(it);
}

public Item getItem(int i)
{
return (Item)catalog.get(i);
}

public int getSize()
{
return catalog.size();
}

public void sort()
{
Collections.sort(catalog);
}
}
编译后生成的两个类文件Item.class和Storefront.class被存放在
G:\JAVA\Package\comm\perfect\ecommerce目录下。
我将classpath设为.;E:\j2sdk14\lib\tools.jar;G:\JAVA\Package,使其指向package包目录。然后创建一个类文件调用到包中的两个类文件,如下
import comm.perfect.ecommerce.*;

public class GiftShop{
public static void main(String[] arguments){
Storefront store=new Storefront();
store.addItem("c01","fmug","19.99","350");
store.addItem("c02","dmug","29.99","250");
store.addItem("c03","smug","39.99","15");
store.addItem("c04","msug","49.99","10");
store.addItem("c05","amug","59.99","50");
store.sort();

for(int i=0;i<store.getSize();i++)
{
Item show=(Item)store.getItem(i);
System.out.println("\nItem ID:"+show.getId()+
"\nName:"+show.getName()+
"\nRetail Price:$"+show.getRetail()+
"\nPrice:$"+show.getPrice()+
"\nQuantity:"+show.getQuantity());
}
}
}

但是编译时总是提示:
--------------------Configuration: j2sdk14 <Default>--------------------
G:\JAVA\GiftShop\GiftShop.java:1: package comm.perfect.ecommerce does not exist
import comm.perfect.ecommerce.*;
^
G:\JAVA\GiftShop\GiftShop.java:5: cannot resolve symbol
symbol : class Storefront
location: class GiftShop
Storefront store=new Storefront();
^
G:\JAVA\GiftShop\GiftShop.java:5: cannot resolve symbol
symbol : class Storefront
location: class GiftShop
Storefront store=new Storefront();
^
G:\JAVA\GiftShop\GiftShop.java:15: cannot resolve symbol
symbol : class Item
location: class GiftShop
Item show=(Item)store.getItem(i);
^
G:\JAVA\GiftShop\GiftShop.java:15: cannot resolve symbol
symbol : class Item
location: class GiftShop
Item show=(Item)store.getItem(i);
^
5 errors

Process completed.
...全文
21 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Winuxava 2002-11-11
  • 打赏
  • 举报
回复
我的机子上能通过啊,难道你的classpath有问题?
try :
g:\java\giftshop\javac -classpath g:\java\package GiftShop.java
g:\java\giftshop\java -classpath g:\java\package GiftShop

如果是win2k,设置了环境变量后先把dos窗口关闭,再开过。
signboy 2002-11-11
  • 打赏
  • 举报
回复
think in java有一章好像特别强调了一些关于包的问题,你可以看一下

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧