原始資料:
書名 : 三國演義
作者 : 羅貫中
售價 : 1000
----------------------------
書名 : 紅樓夢
作者 : 曹雪芹
售價 : 1000
以上,用物件導向載入記憶體中。
Book.java
package javaapplication2;
public class Book {
private String name;
private String author;
private double price;
private final String COMPANY = "三民書局";
public void setPrice(double price) {
if (price > 0) {
this.price = price;
}
}
public double getPrice() {
return price;
}
public void setName(String name) {
if (name != null) {
this.name = name;
}
}
public String getName() {
return name;
}
public void setAuthor(String author) {
if (author != null) {
this.author = author;
}
}
public String getAuthor() {
return author;
}
}
BookMain.java
package javaapplication2;
import javax.swing.JOptionPane;
public class BookMain {
public static void main(String[] args) {
Book book1 = new Book();
Book book2 = new Book();
book1.setName("San Guo");
book1.setAuthor("Luo");
book1.setPrice(1000);
book2.setName("red house dream");
book2.setAuthor("Tsao");
book2.setPrice(1000);
System.out.printf("書名=%s 作者=%s 售價=%.2f%n", book1.getName(), book1.getAuthor(), book1.getPrice());
System.out.printf("書名=%s 作者=%s 售價=%.2f%n", book2.getName(), book2.getAuthor(), book2.getPrice());
}
}