- self变量
class Student():
# name=""
# def study(self):
# print(self.name,'要唱歌去')
# def eat(self):
# print(self.name,"要学习去")
#
# zl=Student()
# zl.name="张良"
# zl.study()
# zl.eat()
-
_init _ ( ) , _str _( )
-
实现
class YangRouChuan():
def __init__(self):
self.cook_time=0
self.status="生的"
def cook(self,cook_time):
self.cook_time+=cook_time
if self.cook_time<=2:
self.status="生的"
elif self.cook_time<4:
self.status="半熟"
elif self.cook_time < 6:
self.status = "熟了"
else:
self.status="焦了"
def __str__(self):
return "烤了%d个小时,状态%s"%(self.cook_time,self.status)
c=YangRouChuan()
c.cook(3)
print(c)
- 类属性和实例属性