通过封装编写Book类

作者在 2012-04-11 21:54:34 发布以下内容

Java作业,拿上来大家看看,看哪些需要改进的~虚心求教···

通过封装编写Book类。要求:类具有属性书名、书号、主编、出版社、出版时间、页数、价格,其中页数不能少于200页,否则输出错误信息,并强制赋默认值200;为各属性设置赋值和取值方法;具有方法detail(),用来在控制台输出每本书的信息。

public class Book {

       private String bookname;

       private int bookid;

       private String bookedi;

       private String bookpub;

       private String bookpt;

       private int bookpage;

       private float bookpric;

public Book(){

       System.out.println("执行Book构造函数:");

}

public String  getter(String name,String id,String editor,String puc, String date,int page,double price){

        return("图书名 图书号 主编 出版商 出版时间 页数 价格 ");    

        }

public void setbookname(String bookname){

       this.bookname=bookname;

}

public void setbookid(int bookid){

       this.bookid=bookid;

}

public void setbookedi(String bookedi){

       this.bookedi=bookedi;

}

public void setbookpub(String bookpub){

       this.bookpub=bookpub;

}

public void setbookpt(String bookpt){

       this.bookpt=bookpt;

}

public void setbookpage(int bookpage){

       this.bookpage=bookpage;

       if (bookpage<200){

              System.out.println("错误!最少页数为200");

              this.bookpage=200;

              }

       else this.bookpage=bookpage;

       }

public void setbookpric(float bookpric){

       this.bookpric=bookpric;

      }

 

 

public String printBook(){

       return "图书名:"+bookname+"\n 图书号:"+bookid+"\n 主编:"+bookedi+"\n出版商:"+bookpub+

       "\n出版时间:"+bookpt+"\n页数:"+bookpage+"\n价格:"+bookpric;

       }

 

 

void detail(){

System.out.println("图书名:" + bookname);

       System.out.println("图书号:" + bookid);

       System.out.println("主编:" + bookedi);

       System.out.println("出版商:" + bookpub);

       System.out.println("出版时间:" + bookpt);

       System.out.println("页数:" + bookpage);

       System.out.println("价格:" +bookpric+"");

}

 

/*import javax.swing.JOptionPane;*/

public class BookView extends Book{

       private String bookname;

       private int bookid;

       private String bookedi;

       private String bookpub;

       private String bookpt;

       private int bookpage;

       private float bookpric;

       public BookView(){

              super();

              }

}

       public static void main(String args[]){

              Book b=new Book();

              b.bookname="JAVA程序开发";

              b.bookid=112;

              b.bookedi="张三";

              b.bookpub="清华大学出版社";

              b.bookpt="2011.01";

              b.bookpage=413;

              b.bookpric=39;

              b.detail();

       }

}

程序 | 阅读 2941 次
文章评论,共0条
游客请输入验证码
浏览20781次