JAVA写在包xhu.soft中定义一个教材类,包含编号、书名、作者、出版社和价格等属性,要求能用构造方法初始化图书 信息;同时能修改图书的各信息。然后在包xhu.test包中编写图书的测试类,输出图书信息。
时间: 2024-09-17 22:04:31 浏览: 44
首先,在`xhu.soft`包中定义一个`Book`类,用于表示教材:
```java
package xhu.soft;
public class Book {
private String bookId;
private String title;
private String author;
private String publisher;
private double price;
// 构造方法,初始化图书信息
public Book(String bookId, String title, String author, String publisher, double price) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
// 提供getter和setter方法,以便修改图书信息
public String getBookId() { return bookId; }
public void setBookId(String bookId) { this.bookId = bookId; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
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 double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
```
接下来,在`xhu.test`包中编写一个`BookTest`类,用于测试图书信息:
```java
package xhu.test;
import xhu.soft.Book;
public class BookTest {
public static void main(String[] args) {
// 创建一个新的Book对象并初始化信息
Book book1 = new Book("B001", "Java Programming", "John Doe", "XYZ Publishers", 49.99);
// 输出初始图书信息
System.out.println("Book Information:");
System.out.println("ID: " + book1.getBookId());
System.out.println("Title: " + book1.getTitle());
System.out.println("Author: " + book1.getAuthor());
System.out.println("Publisher: " + book1.getPublisher());
System.out.println("Price: " + book1.getPrice());
// 修改图书信息并再次输出
book1.setTitle("Advanced Java");
book1.setPrice(59.99);
System.out.println("\nModified Book Information:");
System.out.println("ID: " + book1.getBookId());
System.out.println("Title: " + book1.getTitle());
System.out.println("Author: " + book1.getAuthor());
System.out.println("Publisher: " + book1.getPublisher());
System.out.println("Price: " + book1.getPrice());
}
}
```
当你运行`BookTest`类,它会显示原始的和修改后的图书信息。
阅读全文