class Person():
def __init__(self,name,weight):
self.name = name
self.weight = weight
def __str__(self):
return '我的名字叫 %s 体重是 %.2f' %(self.name,self.weight)
def run(self):
print('%s爱跑步' %self.name)
self.weight -= 0.5
def eat(self):
print('%s吃东西' %self.name)
self.weight += 1
Lilei = Person('李雷',75.0)
Lilei.run()
print(Lilei)
Lilei.eat()
print(Lilei)
示例:
class Animal():
def eat(self):
print('吃')
def drink(self):
print('喝')
def run(self):
print('跑')
def sleep(self):
print('睡')
class Cat(Animal):
def shout(self):
print('喵')
fentiao = Cat()
fentiao.eat()
fentiao.drink()
fentiao.run()
fentiao.sleep()
fentiao.shout()
class Animal():
def eat(self):
print('吃')
def drink(self):
print('喝')
def run(self):
print('跑')
def sleep(self):
print('睡')
class Cat(Animal):
def shout(self):
print('喵')
class Hellokitty(Cat):
def speak(self):
print('我可以说日语')
def shout(self):
print('@#$%%@$#@#@$')
kt = Hellokitty()
kt.shout()
class A():
def test(self):
print('A --- test方法')
def demo(self):
print('A --- demo方法')
class B():
def test(self):
print('B --- test 方法')
def demo(self):
print('B --- demo方法')
class C(A,B):
pass
c = C()
c.test()
c.demo()
object是Python为所有对象提供的基类,提供有一些内置的属性和方法
,可以使用dir函数查看
新式类:以object为基类的类,推荐使用
经典类:不以object为基类的类,不推荐使用
在python3.X中定义的类时,如果没有指定父类,会默认使用object作>为基类–python3.x中定义的类都是新式类
在python2.x中定义类时,如果没有指定父类,则不会以object作为基>类
为保证编写的代码能够同时在python2.x和python3.x运行
今后在定义类时,如果没有父类,建议统一继承自object
class A(object):
pass
#子类和父类存在相同方法时,子类会覆盖父类方法
#运形时总会调用子类方法–> 多态
class Animal(object):
def run(self):
print('running...')
def cry(self):
print('crying...')
class Dog(Animal):
def run(self):
print('dog running...')
def eat(self):
print('dog eating...')
class Cat(Animal):
def run(self):
print('cat running...')
cat = Cat()
cat.run()
dog = Dog()
dog.run()
图书管理系统
1.查询图书
2.增加图书
3.借阅图书
4.归还图书
5.退出系统
书:书名,作者,状态,位置
管理系统:
class Book(object):
def __init__(self, name, author, status, bookindex):
self.name = name
self.author = author
self.status = status
self.bookindex = bookindex
def __str__(self):
if self.status == 1:
stats = '未借出'
elif self.status == 0:
stats = '已借出'
else:
stats = '状态异常'
return '书名: 《%s》 作者: %s 状态: <%s> 位置: %s' \
% (self.name, self.author, stats, self.bookindex)
class BookManage(object):
books = []
def start(self):
self.books.append(Book('python', 'guido', 1, 'ISO9001'))
self.books.append(Book('c', '谭浩强', 1, 'NFS8102'))
self.books.append(Book('java', 'westos', 1, 'PKA7844'))
# 0:借出 1:存在
# python 1
# c 1
# java 1
def Menu(self):
self.start()
while True:
print("""
图书管理系统
1.查询图书
2.增加图书
3.借阅图书
4.归还图书
5.退出系统
""")
choice = input('请选择:')
if choice == '1':
self.showAllBook()
elif choice == '2':
self.addBook()
elif choice == '3':
self.borrowBook()
elif choice == '4':
self.returnBook()
elif choice == '5':
print('欢迎下次使用...')
exit()
else:
print('请输入正确选择')
continue
def showAllBook(self):
for book in self.books:
print(book)
def addBook(self):
name = input('图书名称:')
self.books.append(Book(name, input('作者:'), 1, input('存储位置:')))
print('图书《%s》增加成功' % name)
def checkBook(self, name):
for book in self.books:
if book.name == name:
return book
else:
return None
def borrowBook(self):
name = input('借阅图书名称: ')
ret = self.checkBook(name)
print(ret)
if ret != None:
if ret.status == 0:
print('书籍《%s》已经借出' % name)
else:
ret.status = 0
print('书籍《%s》借阅成功' % name)
else:
print('书籍《%s》不存在' % name)
def returnBook(self):
name = input('归还图书名称:')
ret = self.checkBook(name)
if ret != None:
if ret.status == 0:
ret.status = 1
print('书籍《%s》归还成功' % name)
print(ret)
else:
print('书籍《%s》未借出' % name)
else:
print('书籍《%s》不存在' % name)
manager = BookManage()
manager.Menu()