图书管理系统
面向过程的三个特性:
封装(提高代码重用,降低代码冗余)
def search_book(title)
print('搜索包含关键字{}的图书'.format(title))
book = {'title': 'python 入门', 'price':39.00,'author':search_book}
print(book.get('price',0.0))
book.get('search_book')('python')
步骤:
1.面向对象分析:OOA
2.类定义对象代码模板(蓝图)uml关系类图(把分析对象编程代码)OOD
3.实例化(创建内存对象)OOP
实现:
1.分析对象的特征和行为
2.写类描述对象模板
3.实例化,模拟过程
继承 多态
代码:
import datetime
class Book: //类名大写字母开头
def __init__(self,title,price=0.0,author=' ',publisher= None,pubdate=datetime.date.today()): //初始化器
self.title = title
self.price = price
self.author = author
self.publisher = publisher
self.pubdate = pubdate
def __repr__(self):
return '<图书{}>'.format (self.title)
def print_info(self):
print('当前这本书的信息如下:')
print('标题:{}'. format(self.title))
print('定价:{}'. format(self.price))
print('作者:{}'. format(self.author))
print('出版社:{}'. format(self.publisher))
print('出版时间:{}'. format(self.pubdate))
book1 = Book('c#经典', 29.9,'Tom','优品课堂',date(2016,3,1)) //实例化
book2 = Book('入门到精通')
book2.author='优品'
book1.print_info()