
class washer():
def wash(self):
print("I can wash clothes")
haier=washer()
print(haier) # <__main__.washer object at 0x000002325A4D3FA0>
haier.wash() # I can wash clothes
self
self指的是调⽤该函数的对象。
class washer():
def wash(self):
print("I can wash clothes")
print(self)
haier1=washer()
haier2=washer()
haier1.wash() #<__main__.washer object at 0x0000018021C3EC10>
print(haier1) #<__main__.washer object at 0x0000018021C3EC10>
haier2.wash() #<__main__.washer object at 0x0000018021C3EC70>
print(haier2) #<__main__.washer object at 0x0000018021C3EC70>
类外面处理对象属性
添加+获取
haier1.width=50
print(f"haier's width is {haier1.width}")
# haier's width is 50
类内处理对象属性
class washer():
def print_info(self):
print(f"haier's width is {self.width}")
haier1=washer()
haier1.width=50
haier1.print_info()
# haier's width is 50
魔方方法
__init__()
初始化对象
class washer():
def __init__(self):
self.width=50
def print_info(self):
print(f"haier's width is {self.width}")
haier1=washer()
haier1.print_info()
# haier's width is 50
带参数的__init__()
class washer():
def __init__(self,width):
self.width=width
def print_info(self):
print(f"haier's width is {self.width}")
haier1=washer(50)
haier1.print_info()
# haier's width is 50
__str__()
当使⽤print输出对象的时候,默认打印对象的内存地址。如果类定义了 str ⽅法,那么就会打印从在这个⽅法中 return 的数据。
class washer():
def __init__(self,width):
self.width=width
def print_info(self):
print(f"haier's width is {self.width}")
def __str__(self):
return "This is haier's specification"
haier1=washer(50)
print(haier1)#This is haier's specification
__del__
当删除对象时,python解释器也会默认调⽤ del() ⽅法。
class washer():
def __init__(self,width=50):
self.width=width
def print_info(self):
print(f"haier's width is {self.width}")
def __str__(self):
return "This is haier's specification"
def __del__(self):
print(f"{self} object have been deleted already")
haier1=washer(50)
del haier1
# This is haier's specification object have been deleted already
这里为什么会这样呢 ,去掉str试试看
class washer():
def __init__(self,width=50):
self.width=width
def print_info(self):
print(f"haier's width is {self.width}")
#def __str__(self):
#return "This is haier's specification"
def __del__(self):
print(f"{self} object have been deleted already")
haier1=washer(50)
del haier1
# <__main__.washer object at 0x0000020C25FEAE80> object have been deleted already
__str__会把目标对象替换成return后的语句
综合应用 烤地瓜class SweetPotato():
def __init__(self):
self.cook_time = 0
self.cook_static = "raw"
self.cook_condiments = []
def cook(self, time):
self.cook_time += time
if 0 <= self.cook_time < 3:
self.cook_static = "raw"
elif 3 <= self.cook_time < 5:
self.cook_static = "halfcooked"
elif 5 <= self.cook_time < 8:
self.cook_static = "baked"
elif 8 <= self.cook_time:
self.cook_static = "burnt"
def __str__(self):
return f"This sweet potato have been baked for {self.cook_time} minutes,condition is {self.cook_static},"
f"the added condiments are {self.cook_condiments}"
def add_condiments(self,condiments):
self.cook_condiments.append(condiments)
sp1=SweetPotato()
sp1.cook(5)
sp1.add_condiments("chili")
print(sp1)
# This sweet potato have been baked for 5 minutes,condition is 'baked',the added condiments are ['chili']
搬家具
class Furniture():
def __init__(self,name,area):
self.name=name
self.area=area
def __str__(self):
return self.name
class Home():
def __init__(self,addr,area):
self.addr=addr
self.area=area
self.free_area=area
self.furnitures=[]
def __str__(self):
return f"home situated {self.addr},area is {self.area},free_area is {self.free_area},furnitures are {self.furnitures}"
def add_furnitures(self,item):
if self.free_area > item.area:
self.furnitures.append(item.name)
print(f"{item.name} placed successfully")
self.free_area-=item.area
else:
print(f"no enough space to put {item}")
sofa=Furniture("sofa",3)
desk=Furniture("desk",2)
baskbool_court=Furniture("baskbool_court",500)
home=Home("HangZhou",200)
home.add_furnitures(sofa)
home.add_furnitures(desk)
home.add_furnitures(baskbool_court)
print(home)
# #sofa placed successfully
# desk placed successfully
# no enough space to put baskbool_court
# home situated HangZhou,area is 200,free_area is 195,furnitures are ['sofa', 'desk']