导入
面向对象编程超越过程式编程的必要性
理解面向对象编程(OOP)是什么以及为什么需要它。
数据存储在独立的列表中
titles = ["1984", "The Hobbit"] statuses = ["Available", "Checked Out"]
显示书籍的函数
def display_books(): for i in range(len(titles)): status = statuses[i] print(titles[i] + " - " + status)
显示书籍
print("初始图书馆状态:") display_books()
向图书馆添加新书
1# 添加新书但未更新状态 2titles.append("Dune") # 忘记更新 `statuses`
1print("\n添加 'Dune' 后:") 2display_books()
1# 借阅书籍的函数 2def borrow_book(book_title): 3 if book_title in titles: 4 index = titles.index(book_title) 5 statuses[index] = "Checked Out" # 如果索引缺失,可能会导致错误 6 else: 7 print("书籍 " + book_title + " 在图书馆中未找到。")
1# 尝试借阅新书 2try: 3 borrow_book("Dune") 4except IndexError as e: 5 print("错误:", e)
Coming soon to support pageflux-note
1book1 = ("1984", "Available", "George Orwell") 2book2 = ("The Hobbit", "Checked Out", "J.R.R. Tolkien")
代码示例
1library = [book1, book2]
1for book in library: 2 print("书名:", book[0], "| 状态:", book[1], "| 作者:", book[2])
代码示例
1书名: 1984 | 状态: Available | 作者: George Orwell 2书名: The Hobbit | 状态: Checked Out | 作者: J.R.R. Tolkien
1def borrow_book_tuple(library, title): 2 # 遍历图书馆(书籍列表),`i` 为索引,`book` 为包含书籍信息的元组 3 for i, book in enumerate(library): 4 # 检查当前书籍的标题是否与请求的标题匹配 5 if book[0] == title: 6 # 如果找到书籍,检查其是否可用(book[1] 表示状态) 7 if book[1] == "Available": 8 # 如果书籍可用,则将其状态更新为 "Checked Out" 9 # 由于元组不可变,需创建一个新的元组 10 library[i] = (book[0], "Checked Out", book[2]) 11 print(title, "借阅成功。") # 打印确认消息 12 else: 13 # 如果书籍已被借出(不可用),则提示已借出 14 print(title, "已借出。") 15 return # 一旦找到并处理了书籍,就退出函数 16 # 如果在图书馆中未找到指定标题的书籍,则打印提示信息 17 print(title, "在图书馆中未找到。")
...
图书馆的对象导向编程表示
1# 步骤1:创建 Book 类并定义属性 2class Book: 3 def __init__(self, title, status="Available"): 4 self.title = title 5 self.status = status
1class Book: 2 def __init__(self, title, status="Available"): 3 self.title = title 4 self.status = status 5 6 def display(self): 7 print(self.title + " - " + self.status)
1class Book: 2 def __init__(self, title, status="Available"): 3 self.title = title 4 self.status = status 5 6 def display(self): 7 print(self.title + " - " + self.status) 8 9 def borrow(self): 10 if self.status == "Available": 11 self.status = "Checked Out" 12 print(self.title + " 已被借出。") 13 else: 14 print(self.title + " 已经被借出。")
1class Book: 2 def __init__(self, title, status="Available"): 3 self.title = title 4 self.status = status 5 6 def display(self): 7 print(self.title + " - " + self.status) 8 9 def borrow(self): 10 if self.status == "Available": 11 self.status = "Checked Out" 12 print(self.title + " 已被借出。") 13 else: 14 print(self.title + " 已经被借出。") 15 16# 创建书籍对象 17book1 = Book("1984") 18book2 = Book("The Hobbit", "Checked Out") 19book3 = Book("Dune") # 添加新书
1class Book: 2 def __init__(self, title, status="Available"): 3 self.title = title 4 self.status = status 5 6 def display(self): 7 print(self.title + " - " + self.status) 8 9 def borrow(self): 10 if self.status == "Available": 11 self.status = "Checked Out" 12 print(self.title + " 已被借出。") 13 else: 14 print(self.title + " 已经被借出。") 15 16book1 = Book("1984") 17book2 = Book("The Hobbit", "Checked Out") 18book3 = Book("Dune") 19 20# 显示书籍 21book1.display() 22book2.display() 23book3.display()
1class Book: 2 def __init__(self, title, status="Available"): 3 self.title = title 4 self.status = status 5 6 def display(self): 7 print(self.title + " - " + self.status) 8 9 def borrow(self): 10 if self.status == "Available": 11 self.status = "Checked Out" 12 print(self.title + " 已被借出。") 13 else: 14 print(self.title + " 已经被借出。") 15 16book1 = Book("1984") 17book2 = Book("The Hobbit", "Checked Out") 18book3 = Book("Dune") 19 20# 借阅书籍 21book3.borrow() 22book3.display()