当前位置: 代码迷 >> Java Web开发 >> 关于同一个包中,两个类的调用。出项有关问题
  详细解决方案

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

热度:7   发布时间:2016-04-17 11:11:47.0
关于同一个包中,两个类的调用。。。。出项问题
我在myeclipse下新建了一个package,在package下新建了两个类:具体代码如下:
Java code
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;    }    }


Java code
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。。。为何报没有定义啊????

------解决方案--------------------
把private BookBean book=null;中的=null去掉试试
------解决方案--------------------
CartItemBean 类中book 对象你实例化了没有哦!
------解决方案--------------------
实例化对象是应该用带参数的构造函数。

BookBean bb = new BookBean(。。。)
CartItemBean cib = new CartItemBean(bb);
cib.getItemPrice();
  相关解决方案