class Cat:
def__init__(self,name,age):
self.name = name
self.age = age
#魔术方法,妈的,优达的课真JB坑,这都不讲,类似java toString方法
def__str__(self):
return"姓名:%s年龄:%d"% (self.name,self.age)
defeat(self):
print("cat eat shit")
defdrink(self):
print("cat drink shit")
defintroduce(this):
"""方法的第一个参数都指向本对象自身,可以叫任何名字,默认self"""
print("%s的年龄是%d"% (this.name,this.age))
# tom = Cat()
# tom.drink()
# tom.eat()
# tom.name = "tom" #居然可以在没有定义属性的时候直接给属性赋值
# tom.age = 23
# tom.introduce()
tom = Cat("lanmao",33)
# tom.introduce()
print(tom)
#============================================================================================
class SweetPotato:
def__init__(self):
self.cookedString ="生的"
self.cookedLevel =0
self.smell = []
def__str__(self):
return"地瓜状态:%s(%d)味道是:%s"% (self.cookedString,
self.cookedLevel,str(self.smell))
defcook(self,cook_time):
self.cookedLevel += cook_time
ifself.cookedLevel >=0andself.cookedLevel <3:
self.cookedString ="生的"
elifself.cookedLevel >=3andself.cookedLevel <5:
self.cookedString ="半生不熟"
elifself.cookedLevel >=5andself.cookedLevel <8:
self.cookedString ="熟了"
else:
self.cookedString ="胡了"
defaddCondiments(self,flavor):
self.smell.append(flavor)
digua = SweetPotato()
digua.cook(1)
print(digua)
digua.cook(1)
print(digua)
digua.cook(1)
digua.addCondiments("屎")
print(digua)
digua.cook(1)
print(digua)
digua.cook(1)
print(digua)
digua.cook(1)
digua.addCondiments("姜")
print(digua)
digua.cook(1)
print(digua)
digua.cook(1)
print(digua)