关于同一个包中,两个类的调用。。。。出项问题

NEE1988 2011-11-28 02:32:06
我在myeclipse下新建了一个package,在package下新建了两个类:具体代码如下:

package db;
import java.io.Serializable;

public class BookBean implements Serializable
{
private static final long serialVersionUID = 1L;
private int id;
private String title;
private String author;
private String bookconcern;
private String publish_date;
private float price;
private int amount;
private String remark;

public BookBean()
{
}

public BookBean(int id, String title, String author, String bookconcern,
String publish_date, float price, int amount, String remark)
{
this.id=id;
this.title=title;
this.author=author;
this.bookconcern=bookconcern;
this.publish_date=publish_date;
this.price=price;
this.amount=amount;
this.remark=remark;
}

public int getId()
{
return id;
}

public void setTitle(String title)
{
this.title = title;
}

public void setAuthor(String author)
{
this.author = author;
}

public void setBookconcern(String bookconcern)
{
this.bookconcern = bookconcern;
}

public void setPublish_date(String publish_date)
{
this.publish_date = publish_date;
}

public void setPrice(float price)
{
this.price = price;
}

public void setAmount(int amount)
{
this.amount = amount;
}

public void setRemark(String remark)
{
this.remark = remark;
}

public String getTitle()
{
return title;
}

public String getAuthor()
{
return author;
}

public String getBookconcern()
{
return bookconcern;
}

public String getPublish_date()
{
return publish_date;
}

public float getPrice()
{
return price;
}

public int getAmount()
{
return amount;
}

public String getRemark()
{
return remark;
}

}



package db;

import java.io.Serializable;

public class CartItemBean implements Serializable
{
private BookBean book=null;
private int quantity=0;

public CartItemBean()
{
}

public CartItemBean(BookBean book)
{
this.book=book;
this.quantity=1;
}

public void setBook(BookBean book)
{
this.book = book;
}

public BookBean getBook()
{
return book;
}

public void setQuantity(int quantity)
{
this.quantity = quantity;
}

public int getQuantity()
{
return quantity;
}


public float getItemPrice()
{
float price=book.getPrice()*quantity;
long val=Math.round(price*100);
return val/100.0f;
}
}

在CartItemBean类中调用book.getPrice()方法,为何报错啊:The method getPrice() is undefined for the type BookBean。。。。在同一个包中,都是public。。。为何报没有定义啊????
...全文
177 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
菖蒲老先生 2011-11-28
  • 打赏
  • 举报
回复
实例化对象是应该用带参数的构造函数。

BookBean bb = new BookBean(。。。)
CartItemBean cib = new CartItemBean(bb);
cib.getItemPrice();
lzq_it 2011-11-28
  • 打赏
  • 举报
回复
CartItemBean 类中book 对象你实例化了没有哦!
老猫的TOM 2011-11-28
  • 打赏
  • 举报
回复
把private BookBean book=null;中的=null去掉试试

81,090

社区成员

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

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