![图片[1]-python怎么调用方法-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5e73566b98911586.jpg)
类的方法的定义
1、定义一个类
deffun_name(self,...); Pass
2.其中的参数self代表类的实例,在调用方法时由系统自动提供
3.方法定义时必须指明self参数
类的方法的调用
与普通的函数调用类似
1.类的内部调用:self.<方法名>(参数列表)。
2.在类的外部调用:<实例名>.<方法名>(参数列表)。
注意:以上两种调用方法中,提供的参数列表中都不用包括self。
wash.py
classWasher:
def__init__(self):
self.water=0
self.scour=0
defadd_water(self,water):
print('Addwater:',water)
self.water=water
defadd_scour(self,scour):
self.scour=scour
print('Addscour:',self.scour)
defstart_wash(self):
print('Startwash...')
if__name__=='__main__':
w=Washer()
w.add_water(10)
w.add_scour(2)
w.start_wash()原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END















































暂无评论内容