查看完整版本: [-- 关于包的问题:一个例子大家帮我看看为什么在Item那里出了错误 --]

风信Java论坛 ›› Java 基础开发 ›› 关于包的问题:一个例子大家帮我看看为什么在Item那里出了错误 登录 -> 注册

1F 关于包的问题:一个例子大家帮我看看为什么在Item那里出了错误   tyrone0070 Post by : 2009-04-14 17:06:34.0

首先我创建了一个叫com.prefect.ecommerce的包
在包里面创建了源文件Item.java文件如下
package com.prefect.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.java(问题1:这个类我需不需要放在com.prefect.ecommerce里面?)创建的类的源代码如下
package com.prefect.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);
}

}
最后我写了个程序用到了包,程序名Giftshop.java程序如下
import com.prefect.ecommerce.*;

public class Giftshop{

public static void main(String[] arguments){
Storefront store=new Storefront();
store.addItem("C01", "MUG", "9.99", "150");
store.addItem("C02", "LG MUG", "12.99", "82");
store.addItem("C03", "MOUSEPAD", "10.49", "800");
store.addItem("D01", "T SHIRT", "16.99", "90");

for(int i=0;i<store.getSize();i++){
Item show =(Item)store.getItem(i);//这里出现了错误说Cannot cast from com.prefect.ecommerce.Item to Item?我不知道哪里写错了这里应该是不能导入Item
System.out.println("\nItem ID: "+show.getId()+
"\nName"+show.getName()+
"\nRetail Price: $"+show.getRetail()+
"\nPrice: $"+show.getPrice()+
"\nQuantity: "+show.getQuantity());

}

}
}

2F    唧唧 Post by : 2009-04-14 17:51:45.0
回楼主:
第一个问题,Storefront.java 应该放到 com.prefect.ecommerce里面,因为你的类文件已经指定了所在包!(package com.prefect.ecommerce; );

其中 Giftshop.java 我能正确执行,执行结果是:

Item ID: C01
NameMUG
Retail Price: $9.99
Price: $6.99
Quantity: 150

Item ID: C02
NameLG MUG
Retail Price: $12.99
Price: $9.09
Quantity: 82

Item ID: C03
NameMOUSEPAD
Retail Price: $10.49
Price: $5.25
Quantity: 800

Item ID: D01
NameT SHIRT
Retail Price: $16.99
Price: $11.89
Quantity: 90
3F    唧唧 Post by : 2009-04-14 17:54:03.0
PS: 我是用 Eclipse 调试的,你是用什么IDE调试呢?

如果使用cmd命令符调试,就可能出现导入不到类的问题,检查 环境变量 的classpath 是不是以 .; 开始!
4F Re:Re:~   tyrone0070 Post by : 2009-04-14 22:27:44.0

感谢你啊,已经弄好了,是我的环境变量没有设置好。。。。。。

 


风信Java论坛 ›› Java 基础开发 ›› 关于包的问题:一个例子大家帮我看看为什么在Item那里出了错误 登录 -> 注册

查看完整版本: [-- 关于包的问题:一个例子大家帮我看看为什么在Item那里出了错误 --]
CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 8 ms,0 (Queries)  Gzip enabled
粤ICP备07511478号