1.Person():实例化的实质是:
__new__: 创建一个新的对象
__init__:对上边创建的对象进行初始化
要给Person类定义三个对象属性,name, gender, age
__new__:创建一个新的对象
__init__:去给对象的这个三个属性赋值
zhangsan —> name=zhangsan, gender:male, age:20
lisi -> name=lisi, gender:male age:18
zhangsan = Person("zhangsan", "male", 20)
lisi = Person("lisi", "male", 18)
zhangsan.print_info()
lisi.print_info()
1.定义一个Book类:
定义类属性:count
定义对象属性或变量: title(书名),author(作者),publish (出版社),price(价格)
定义对象相加操作: book1 + book2 = bookl.title + book2.title
举例: book1 + book2 = Java程序设计Pvthon程序设计
定义打印对象的输出:使用print打印 book1 => 书的名字-作者-出版社-价格
举例: print (book1) => Python程序设计-吉米勒-机械出版社-35
定义调用对象的方法:__call__(): 并让其返回书的名字ca11
定义静态方法: static_print_obj: 执行输出print("This is Static Method of class Book")
定义类方法: class_print_obi: 执行输出print("This is class Method of class Book")
class Book:
count = None
def __init__(self, title, author, publish, price):
self.title = title
self.author = author
self.publish = publish
self.price = price
def __add__(self, other):
return self.title + other.title
def __str__(self):
return self.title + "-" + self.author + "-" + self.publish + "-" + str(self.price)
def __call__(self, *args, **kwargs):
return self.title
@staticmethod
def static_print_obj():
print("Thia is Static Method of class BOOk")
@classmethod
def class_print_obj(cls):
print("This is class Method of class Book")
book1 = Book("Python程序设计","王铮", "机械教育出版社",22)
book2 = Book("Java程序设计", "李刚", "清华大学出版社", 34)
print(book1 + book2)
print(book1)
print(book1())
Book.count = 100
print(Book.count)
print(book1.count)
book1.title = "流畅的python"
print(book1.title)
Book.static_print_obj()
Book.class_print_obj()
book1.static_print_obj()
book1.class_print_obj()