当前位置: 技术问答>java相关
关于package包的问题,请高手指教
来源: 互联网 发布时间:2015-10-29
本文导语: 我在g:盘java目录下建立package目录作为自定义包目录。即g:javapackage作为存储包的目录。 然后定义Item类和Storefront类放在包中代码如下: Item类: package comm.perfect.ecommerce; import java.util.*; public class Item implements Comparab...
我在g:盘java目录下建立package目录作为自定义包目录。即g:javapackage作为存储包的目录。
然后定义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.pricetemp.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:JAVAPackagecommperfectecommerce目录下。
我将classpath设为.;E:j2sdk14libtools.jar;G:JAVAPackage,使其指向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
然后定义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.pricetemp.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:JAVAPackagecommperfectecommerce目录下。
我将classpath设为.;E:j2sdk14libtools.jar;G:JAVAPackage,使其指向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