本文目录导读:

我将为您提供一个完整的Java面向对象编程案例,涵盖核心概念:封装、继承、多态和抽象,这是一个图书馆管理系统的基础版本。
基础类设计
图书类(封装示例)
// 图书类 - 演示封装
public class Book {
// 私有属性(封装)
private String isbn;
private String title;
private String author;
private double price;
private boolean isBorrowed;
// 构造方法
public Book() {}
public Book(String isbn, String title, String author, double price) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.price = price;
this.isBorrowed = false;
}
// Getter 和 Setter 方法
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
if (price >= 0) {
this.price = price;
} else {
System.out.println("价格不能为负数!");
}
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
// 业务方法
public void borrowBook() {
if (!isBorrowed) {
isBorrowed = true;
System.out.println("《" + title + "》借阅成功");
} else {
System.out.println("《" + title + "》已被借出");
}
}
public void returnBook() {
if (isBorrowed) {
isBorrowed = false;
System.out.println("《" + title + "》归还成功");
} else {
System.out.println("《" + title + "》未被借出");
}
}
@Override
public String toString() {
return String.format("ISBN: %s, 书名: %s, 作者: %s, 价格: %.2f元, 状态: %s",
isbn, title, author, price, isBorrowed ? "已借出" : "可借");
}
}
电子书类(继承示例)
// 电子书类 - 演示继承
public class EBook extends Book {
private double fileSize; // 文件大小(MB)
private String format; // 格式(PDF, EPUB等)
public EBook() {
super();
}
public EBook(String isbn, String title, String author, double price,
double fileSize, String format) {
super(isbn, title, author, price);
this.fileSize = fileSize;
this.format = format;
}
// 重写父类方法(多态)
@Override
public void borrowBook() {
if (!isBorrowed()) {
setBorrowed(true);
System.out.println("电子书《" + getTitle() + "》已下载到设备");
} else {
System.out.println("电子书《" + getTitle() + "》下载失败");
}
}
@Override
public String toString() {
return super.toString() + String.format(", 文件大小: %.1fMB, 格式: %s",
fileSize, format);
}
}
杂志类(抽象类示例)
// 抽象类 - 演示抽象
public abstract class Publication {
protected String title;
protected String publisher;
protected int publicationYear;
public Publication(String title, String publisher, int publicationYear) {
this.title = title;
this.publisher = publisher;
this.publicationYear = publicationYear;
}
// 抽象方法
public abstract void displayInfo();
// 具体方法
public String getTitle() {
return title;
}
}
杂志实现类
// 杂志类 - 实现抽象方法
public class Magazine extends Publication {
private int issueNumber; // 期号
private String category; // 类别
public Magazine(String title, String publisher, int publicationYear,
int issueNumber, String category) {
super(title, publisher, publicationYear);
this.issueNumber = issueNumber;
this.category = category;
}
@Override
public void displayInfo() {
System.out.println("杂志信息:");
System.out.println("标题: " + title);
System.out.println("出版社: " + publisher);
System.out.println("出版年份: " + publicationYear);
System.out.println("期号: " + issueNumber);
System.out.println("类别: " + category);
}
}
接口设计(多态示例)
// 可借阅接口
public interface Borrowable {
void borrow();
void returnItem();
}
// 可搜索接口
public interface Searchable {
boolean search(String keyword);
}
// 图书馆类 - 实现多个接口
public class Library implements Borrowable, Searchable {
private String name;
private List<Book> books;
private List<Magazine> magazines;
public Library(String name) {
this.name = name;
this.books = new ArrayList<>();
this.magazines = new ArrayList<>();
}
// 添加图书
public void addBook(Book book) {
books.add(book);
System.out.println("图书添加成功: " + book.getTitle());
}
// 添加杂志
public void addMagazine(Magazine magazine) {
magazines.add(magazine);
System.out.println("杂志添加成功: " + magazine.getTitle());
}
// 实现接口方法
@Override
public void borrow() {
System.out.println("在" + name + "图书馆借阅物品");
}
@Override
public void returnItem() {
System.out.println("归还物品到" + name + "图书馆");
}
@Override
public boolean search(String keyword) {
System.out.println("在" + name + "搜索: " + keyword);
boolean found = false;
for (Book book : books) {
if (book.getTitle().contains(keyword) ||
book.getAuthor().contains(keyword)) {
System.out.println("找到图书: " + book);
found = true;
}
}
if (!found) {
System.out.println("未找到相关资源");
}
return found;
}
// 显示所有藏书
public void displayAllResources() {
System.out.println("\n=== " + name + " 图书馆藏书 ===");
System.out.println("图书列表:");
for (Book book : books) {
System.out.println(book);
}
System.out.println("\n杂志列表:");
for (Magazine magazine : magazines) {
magazine.displayInfo();
System.out.println();
}
}
}
主测试类
// 测试类 - 演示所有概念
public class LibraryManagementSystem {
public static void main(String[] args) {
// 创建图书馆
Library universityLibrary = new Library("大学图书馆");
// 创建普通图书
Book javaBook = new Book("978-7-111-54742-0",
"Java核心技术",
"Cay S. Horstmann",
89.50);
// 创建电子书
EBook webEBook = new EBook("978-7-121-32512-3",
"Web前端开发实战",
"李刚",
69.00,
25.5,
"PDF");
// 创建杂志
Magazine techMagazine = new Magazine(
"科技前沿",
"中国科学院出版社",
2024,
38,
"科技"
);
// 添加资源到图书馆
universityLibrary.addBook(javaBook);
universityLibrary.addBook(webEBook);
universityLibrary.addMagazine(techMagazine);
// 演示多态
System.out.println("\n=== 多态演示 ===");
Book book1 = new Book("ISBN-001", "设计模式", "GoF", 56.00);
Book book2 = new EBook("ISBN-002", "深入理解JVM", "周志明",
78.00, 30.0, "EPUB");
// 多态调用 - 父类引用指向子类对象
testBookBorrow(book1);
testBookBorrow(book2);
// 演示搜索功能
System.out.println("\n=== 搜索功能演示 ===");
universityLibrary.search("Java");
// 演示借阅功能
System.out.println("\n=== 借阅功能演示 ===");
universityLibrary.borrow();
javaBook.borrowBook();
webEBook.borrowBook();
// 显示所有资源
universityLibrary.displayAllResources();
// 演示封装
System.out.println("\n=== 封装演示 ===");
javaBook.setPrice(-50); // 尝试设置非法价格
System.out.println("当前价格: " + javaBook.getPrice());
// 演示接口实现
System.out.println("\n=== 接口演示 ===");
Borrowable borrowable = universityLibrary;
Searchable searchable = universityLibrary;
borrowable.borrow();
borrowable.returnItem();
searchable.search("科技");
}
// 辅助方法 - 演示多态
public static void testBookBorrow(Book book) {
System.out.println("测试借阅: " + book.getTitle());
book.borrowBook();
System.out.println();
}
}
UML类图说明
┌─────────────────────────┐
│ Publication │
├─────────────────────────┤ String │
│ - publisher: String │
│ - publicationYear: int │
├─────────────────────────┤
│ + displayInfo(): void │
└─────────────────────────┘
▲
│
│
┌─────────────────────────┐
│ Magazine │
├─────────────────────────┤
│ - issueNumber: int │
│ - category: String │
└─────────────────────────┘
┌─────────────────────────┐
│ Book │
├─────────────────────────┤
│ - isbn: String │ String │
│ - author: String │
│ - price: double │
│ - isBorrowed: boolean │
├─────────────────────────┤
│ + borrowBook(): void │
│ + returnBook(): void │
└─────────────────────────┘
▲
│
┌─────────────────────────┐
│ EBook │
├─────────────────────────┤
│ - fileSize: double │
│ - format: String │
└─────────────────────────┘
┌─────────────────────────┐
│ Borrowable │
│ (interface) │
├─────────────────────────┤
│ + borrow(): void │
│ + returnItem(): void │
└─────────────────────────┘
▲
│
┌─────────────────────────┐
│ Library │
├─────────────────────────┤
│ - name: String │
│ - books: List<Book> │
│ - magazines: List<Mag> │
└─────────────────────────┘
设计模式应用(进阶)
// 单例模式 - 图书馆管理
public class LibraryManager {
private static LibraryManager instance;
private Map<String, Book> bookMap;
private LibraryManager() {
bookMap = new HashMap<>();
}
public static synchronized LibraryManager getInstance() {
if (instance == null) {
instance = new LibraryManager();
}
return instance;
}
public void addBook(String key, Book book) {
bookMap.put(key, book);
}
public Book getBook(String key) {
return bookMap.get(key);
}
}
运行结果示例
=== 多态演示 ===
测试借阅: 设计模式
《设计模式》借阅成功
测试借阅: 深入理解JVM
电子书《深入理解JVM》已下载到设备
=== 借阅功能演示 ===
在大学图书馆借阅物品
《Java核心技术》借阅成功
电子书《Web前端开发实战》已下载到设备
=== 封装演示 ===
价格不能为负数!
当前价格: 89.5
设计要点总结
- 封装:所有数据成员都设置为私有,通过公共方法访问
- 继承:Book 继承 Publication,EBook 继承 Book
- 多态:父类引用指向子类对象,方法重写
- 抽象:抽象类和接口的使用
- 接口:定义规范,实现多种行为
- 设计模式:单例模式管理图书馆实例
这个案例涵盖了Java面向对象的全部核心概念,适合学习和教学中使用,您可以根据需求扩展功能,如增加读者管理、借阅记录等功能。