classBook(object):def__init__(self, name, author, state, index):
self.name = name ##封装图书信息
self.author = author
self.state = state
self.index = index
def__str__(self):##将0,1状态转化为借出和为借出if self.state == 0:
state = 'IN'else:
state = 'OUT'return'Book(%s,%s,%s,%s)' % (self.name, self.author, state, self.index)
classBookManage(object):
BookList = [] ##图书信息列表,储存对象类型defaddbook(self):##将对象类型的图书信息加入列表"""Add a new Book"""
print('New Book Adding'.center(40, '*'))
name = input('Please input the book name: ')
author = input('Please input the book author: ')
index = input('Please input the book position: ')
self.BookList.append(Book(name, author, 0, index))
print(name + ' is Added Sucessfully!')
defborrowbook(self):"""Borrow Books"""
print('Borrow Book'.center(40, '*'))
name = input('Please input the book name what you want to berrow: ')
for book in self.BookList: ##可调用checkbook函数,效果一样if name == book.name: ##遍历,判断借阅的图书是否存在
book.state = 1##借出之后,状态变为1
print(name + ' is Borrowed OK1')
else:
print(name + ' is not exist!!')
defcheckbook(self, name):##定义checkbook函数,判断图书是否存在"""Check the message of Book"""
print('Book Messages'.center(40, '*'))
for book in self.BookList:
if name == book.name:
return book
else:
returnNonedefusercheck(self):##查找图书信息
name = input('Please input the book name what you want to check: ')
BookRes = self.checkbook(name)
if BookRes:
print(name + ' has been found!')
print(BookRes)
else:
print(name + ' is not exist!!')
defviewbook(self):##遍历显示图书收藏信息"""View the BookList"""
print('View Books'.center(40, '*'))
for book in self.BookList:
print(book)
bm = BookManage() ##实例化
bm.addbook() ##调用类的函数# bm.addbook()
bm.borrowbook()
# bm.usercheck()
bm.viewbook()
2.基础提升版:
优化管理系统,加入循环;
优化图书借阅模块;
函数优化,可移植;
classBook(object):def__init__(self, name, author, state, index):
self.name = name
self.author = author
self.state = state
self.index = index
def__str__(self):if self.state == 0:
state = 'IN'else:
state = 'OUT'return'Book(%s,%s,%s,%s)' % (self.name, self.author, state, self.index)
classBookManage(object):
BookList = []
definitBook(self):##初始化图书信息
self.BookList.append(Book('python', 'Guido', 0, 'PY001'))
self.BookList.append(Book('Java', 'Guido', 1, 'JA001'))
self.BookList.append(Book('Linux', 'Linus', 0, 'LI001'))
defmenu(self):##定义menu函数,进行循环
info = """
BookManage System
1. add
2. borrow
3. check
4. view
5. delete
6. exit
Please input your choice: """whileTrue: ##根据用户的选择执行不同操作
choice = input(info)
if choice == '1':
self.addbook()
elif choice == '2':
self.borrowbook()
elif choice == '3':
self.usercheck()
elif choice == '4':
self.viewbook()
elif choice == '5':
self.deleteBook()
elif choice == '6':
exit(0)
defaddbook(self):##添加图书信息"""Add a new Book"""
print('New Book Adding'.center(40, '*'))
name = input('Please input the book name: ')
author = input('Please input the book author: ')
index = input('Please input the book position: ')
self.BookList.append(Book(name, author, 0, index))
print(name + ' is Added Sucessfully!')
defborrowbook(self):##借阅模块优化"""Borrow Books"""
print('Borrow Book'.center(40, '*'))
name = input('Please input the book name what you want to berrow: ')
BookRes = self.checkbook(name)
if BookRes: ##判断图书是否存在,调用checkbook函数if BookRes.state == 0: ##判断图书状态,是否借出
BookRes.state = 1#3成功借出,更改图书状态
print(name + ' is Borrowed OK1')
else:
print(name + ' is already borrowed!!')
else:
print(name + ' is not exist!!')
defcheckbook(self, name):##判断图书是否存在,返回不同值"""Check the message of Book"""
print('Book Messages'.center(40, '*'))
for book in self.BookList:
if name == book.name:
return book
else:
returnNonedefusercheck(self):##查询图书信息
name = input('Please input the book name what you want to check: ')
BookRes = self.checkbook(name)
if BookRes: ##图书存在,显示当前图书信息
print(name + ' has been found!')
print(BookRes)
else:
print(name + ' is not exist!!')
defviewbook(self):##遍历显示所有图书信息"""View the BookList"""
print('View Books'.center(40, '*'))
for book in self.BookList:
print(book)
defdeleteBook(self):passdefmain():##定义main函数
bm = BookManage() ##实例化
bm.initBook() ##初始化
print('Systemd OK')
bm.menu() ##调用menu函数,进入管理系统if __name__ == '__main__': ##判断的是这个脚本内容是否为被导入的模块内容
main()
五、类操作
1.类属性
1、私有属性:只能在类中使用的属性(双下划线__) self.__state = state
2、私有方法:只能在类中使用的方法(双下划线__) def get__state(self):
代码示例:
classBook(object):def__init__(self, name, author, state, index):
self.name = name
self.author = author
self.__state = state ##私有属性,类外部不能调用
self.index = index
defget__state(self):##私有方法,类外部不能调用if self.__state == 0:
return'IN'if self.__state == 1:
return'OUT'defset__state(self, value):##私有方法,类外部不能调用if value in [0, 1]:
self.__state = value
returnTrueelse: ##raise:抛出异常raise Exception('The State has been in [0/1]')
book1 = Book('linux', 'lee', 0, 'DT3435') ##实例化
book.__sate ##会报错,不能调用state
print(book1.get__state()) ##调用函数繁琐
book1.set__state(3) ##3不符合state要求,会抛出异常
print(book1.get__state())
3、类属性装饰器 @property
classBook(object):def__init__(self, name, author, state, index):
self.name = name
self.author = author
self.__state = state
self.index = index
@property ##代表state不是方法,是一个属性值, book1.statedefstate(self):if self.__state == 0:
return'IN'if self.__state == 1:
return'OUT'@state.setter ##book.state = value 赋值或修改defstate(self, value):if value in [0, 1]:
self.__state = value
returnTrueelse:
raise Exception('The State has been in [0/1]')
book1 = Book('linux', 'lee', 0, 'DT3435') ##实例化
print(book1.state) ##类属性装饰器装饰后,可直接调用,并对state做了限制
book1.state = 1
print(book1.state)