class car():
#初始化
def init(self,name1,color1):
self.name=name1
self.color=color1
#初始位置为零
self.statius1=0
#定义一个名字方法
def cname(self):
return self.name
#定义一个颜色方法
def ccolor(self):
return self.color
#定义位置方法 并将移动距离增加
def statius(self,len1):
self.statius1+=len1
return ‘汽车移动{}米’.format(self.statius1)
#输出返回值
def str(self):
return ‘汽车是{} 颜色是{} 现在的位置是{}’.format(self.name,self.color,self.statius1)
car1=car(‘宝马’,‘红色’)
print(car1)
car1.statius(10)
print(car1)
car1.statius(10)
print(car1)
输出效果:
汽车是宝马 颜色是红色 现在的位置是0
汽车是宝马 颜色是红色 现在的位置是10
汽车是宝马 颜色是红色 现在的位置是20