编写一个 Book类java,用于描述个人藏书,包含私有化属性:作者Author 、 书名 Title 、价格 Price 、出版社 Publisher、出版年份 Published Year 五个属性。然后编写一个测试类BookTest,对 Book 类进行测试输出正确的属性值。
时间: 2024-02-22 18:58:51 浏览: 78
Book.java
好的,以下是 Book 类的 Java 代码:
```java
public class Book {
private String author;
private String title;
private double price;
private String publisher;
private int publishedYear;
public Book(String author, String title, double price, String publisher, int publishedYear) {
this.author = author;
this.title = title;
this.price = price;
this.publisher = publisher;
this.publishedYear = publishedYear;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public String getPublisher() {
return publisher;
}
public int getPublishedYear() {
return publishedYear;
}
}
```
以下是 BookTest 类,用于测试 Book 类:
```java
public class BookTest {
public static void main(String[] args) {
Book book = new Book("J.K. Rowling", "Harry Potter and the Philosopher's Stone", 9.99, "Bloomsbury", 1997);
System.out.println("Author: " + book.getAuthor());
System.out.println("Title: " + book.getTitle());
System.out.println("Price: " + book.getPrice());
System.out.println("Publisher: " + book.getPublisher());
System.out.println("Published Year: " + book.getPublishedYear());
}
}
```
当你运行 BookTest 类时,会输出以下内容:
```
Author: J.K. Rowling
Title: Harry Potter and the Philosopher's Stone
Price: 9.99
Publisher: Bloomsbury
Published Year: 1997
```
阅读全文