日期:2014-05-20  浏览次数:20686 次

本人菜鸟,初学java望高手指点呀。。。。
interface Goods
{
float getPrice(); //定义商品价格抽象方法
String getName(); //定义商品名字抽象方法
}
class ShopCar 
{
  private Goods goods[]; //定义商品数组
int foot; //数组下表
ShopCar(int len) //构造方法
{
  if(len>0)
{
  goods=new Goods[len]; 
}
else
{
goods=new Goods[1];
}
}
public void add(Goods a) //购物车添加商品
{
if(this.foot<goods.length)
{
this.goods[this.foot++]=a;
}
}
  public float check(Goods a[]) //求出购物车的总价格
{
int nCount=0;
  for(int x=0;x<a.length;x++)
{
  nCount+=a[x].getPrice();
}
return nCount;
}
public Goods[] getGoods() //返回当前商品
{
return this.goods;
}
}
class Book implements Goods
{
  float price; //价格
String name; //名称
Book(float price,String name) //构造方法
{
  this.price=price;
this.name=name;
}
public void setPrice(float price) //设置价格
{
  this.price=price;
}
public void setName(float name) //设置姓名
{
this.name=name;
}
public float getPrice() //获取价格
{
return this.price;
}
public String getName() //获取姓名
{
return this.name;
}
}
public class Num6
{
//商品
Goods a=new Book(31.f,"java实习开发");
Goods b=new Book(32.f,"javaWeb开发");
Goods c=new Book(33.f,"ASP.net开发");
//购物车
ShopCar d=new ShopCar(4);
//将商品加入购物车
d.add(a);
  [color=#FF0000]d.add(b); d.add(c);[/color] //所购买的图书
for(int x=0;x<d.getGoods().length;x++)
{
System.out.println("********商品信息*********"+"\n书名:"+d.getGoods()[x].getName());
}
//结账
System.out.println("****商品总价格******\n"+d.check(d.goods));
}
小弟我改了又改。。结果编译还是有错!!!提示说红色字体需要标示符。。望大家指点呀!!!

------解决方案--------------------
d.add(b); d.add(c); //所购买的图书
我想问下楼主 java代码里面 有这样写的么??[color][/color] 这算是一对标签??
------解决方案--------------------
楼主没有搞清楚java类的组成结构,类里面有成员变量跟方法,大致的结构是
Java code
class XXX{
  private XXX xxx;
  public void XXX(){
   //具体的实现
}
}