图书类


********8、 设计一个图书类Book,该Book类具有属性:书籍编号(bookId),书名(bookName),作者(author),出版单位(publisher),单价(price)。
a) 要求属性私有,生成其共有的get/set 方法;
b) 具有方法:show():该方法内显示给书籍的详细信息
c) 在创建一个Book对象的同时给该对象各属性赋初值。(带参构造)
d) 编写测试类TestBook,实现创建一个Book对象book,打印出该书的详细信息。
public class Book {
        private String bookId;
        private String bookName;
        private String author;
        private String publisher;
        private String price;
        public Book(){
        	
        }
        public Book(String bi,String bn,String au,String pu,String pr){
        	this.bookId=bi;
        	this.bookName=bn;
        	this.author=au;
        	this.publisher=pu;
        	this.price=pr;
        	
        }
        void show()
        {
        	System.out.println("书籍编号:"+this.bookId+"书名:"+this.bookName+"作者:"+this.author+"出版单位:"+this.publisher+"价格:"+this.price);
        }
		public String getBookId() {
			return bookId;
		}
		public void setBookId(String bookId) {
			this.bookId = bookId;
		}
		public String getBookName() {
			return bookName;
		}
		public void setBookName(String bookName) {
			this.bookName = bookName;
		}
		public String getAuthor() {
			return author;
		}
		public void setAuthor(String author) {
			this.author = author;
		}
		public String getPublisher() {
			return publisher;
		}
		public void setPublisher(String publisher) {
			this.publisher = publisher;
		}
		public String getPrice() {
			return price;
		}
		public void setPrice(String price) {
			this.price = price;
		}
        

public class TestBook {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 Book book=new Book("20135850","赵大逼正传!","王爷  ","王爷税务局    ","价值连城     ");
 book.show();
	}

}

好的,以下是代码示例: ``` // 基-书籍 class Book { public: Book(string title, string author, int year) : m_title(title), m_author(author), m_year(year) {} virtual ~Book() {} virtual void print() const = 0; // 纯虚函数,输出书籍信息 protected: string m_title; // 书籍名称 string m_author; // 作者 int m_year; // 出版年份 }; // 派生-图书 class BookType : public Book { public: BookType(string title, string author, int year, string type) : Book(title, author, year), m_type(type) {} virtual ~BookType() {} virtual void print() const { cout << "书籍名称:" << m_title << endl; cout << "作者:" << m_author << endl; cout << "出版年份:" << m_year << endl; cout << "图书型:" << m_type << endl; } private: string m_type; // 图书型 }; // 派生-图书 class BookInfo : public BookType { public: BookInfo(string title, string author, int year, string type, string isbn, string publisher) : BookType(title, author, year, type), m_isbn(isbn), m_publisher(publisher) {} virtual ~BookInfo() {} virtual void print() const { cout << "书籍名称:" << m_title << endl; cout << "作者:" << m_author << endl; cout << "出版年份:" << m_year << endl; cout << "图书型:" << m_type << endl; cout << "ISBN:" << m_isbn << endl; cout << "出版社:" << m_publisher << endl; } private: string m_isbn; // ISBN编码 string m_publisher; // 出版社 }; ``` 在上面的代码中,Book一个BookType和BookInfo是继承自Book的派生Book中定义了一个纯虚函数print(),表示输出书籍信息。BookType和BookInfo分别在print()函数中输出了不同的信息。这样就达到了使用继承关系创建不同型的书籍的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值