完成一个类的封装
1.完整的定义一个book的类,在类中可能存在很多应该定义的属性
2.init() 方法来定义函数有什么
class Book:
'''init() 方法来定义函数有什么'''
def __init__(self,title,price,author,publish):
self.title = title
self.price=price
self.author=author
self.publish=publish
'''新定义的方法可以来表示函数可以完成什么操作
def printMsg(self):
print(self.title)
print(self.publish)
print(self.author)
print(self.price)
print(self.author)
'''实例化对象,用构造函数的方法'''
'''在传参中不需要传入self的参数值'''
book = Book('Python入门',100.00,'Tommy','清华大学出版社')
print(book.printMsg())
运用多种内置的函数方法
def init :完成预定义的构造器
def repr(self): 直接写出返回值
def __repr__(self):
return '<图书:{} at 内存中的位置>。'.format(self.title,id(self))
def del(self): 删除其中的某一个属性
def __del__(self):
'''Unexcept indent 表示一个意外的缩进'''
'''这个在用的时候不需要return'''
BookMsg.count-=1
def __str__(self):
return '[图书:{} at 内存中的位置 0x{}]。'