编写一个Book类,用于描述个人藏书,包含私有化属性:作者Author、书名Title、价格Price、出版社Publisher、出版年份Published Year五个属性。然后编写一个测试类BookTest,对Book类进行测试,输出正确的属性值。Java
时间: 2024-02-06 16:04:29 浏览: 99
好的,以下是Book类的代码:
```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测试类的代码:
```java
public class BookTest {
public static void main(String[] args) {
Book book = new Book("J.K. Rowling", "Harry Potter and the Philosopher's Stone", 29.99, "Bloomsbury Publishing", 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());
}
}
```
输出结果为:
```
Author: J.K. Rowling
Title: Harry Potter and the Philosopher's Stone
Price: $29.99
Publisher: Bloomsbury Publishing
Published Year: 1997
```
阅读全文