创建一个图书类,属性bookName,出版社publisher,作者author,价格price拥有方法:属性对应的get和set方法和介绍这一本图书。 创建一个类 人 去借图书
时间: 2024-11-18 08:15:42 浏览: 31
好的,首先我们创建一个`图书`类,它包含了书名、出版社、作者和价格等属性,以及对应的getter和setter方法,还有介绍图书的方法:
```java
public class Book {
private String bookName;
private String publisher;
private String author;
private double price;
// 构造函数
public Book(String bookName, String publisher, String author, double price) {
this.bookName = bookName;
this.publisher = publisher;
this.author = author;
this.price = price;
}
// Getters and setters
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// 介绍图书的方法
public String introduceBook() {
return "这是一本名为《" + bookName + "》的图书,由" + author + "著,来自出版社:" + publisher + ",定价:" + price + "元。";
}
}
// 接下来我们创建一个人类 `Person` 类,包含借书的方法
public class Person {
private String name; // 假设这里有一个姓名属性
// 借书方法
public void borrowBook(Book book) {
System.out.println(name + "想借阅:" + book.introduceBook());
// 这里可以添加实际的借书逻辑,比如检查库存等
}
}
```
现在,你可以创建`Book`实例并通过`Person`实例的`borrowBook`方法去借书。例如:
```java
Book myBook = new Book("Java编程", "人民邮电出版社", "张三", 49.0);
Person reader = new Person();
reader.borrowBook(myBook); // 输出读者的信息和书籍介绍
```
阅读全文