本文摘自php中文网,作者尚,侵删。

类的方法的调用:
与普通的函数调用类似
1、类的内部调用:self.<方法名>(参数列表)。
2、在类的外部调用:<实例名>.<方法名>(参数列表)。
注意:以上两种调用方法中,提供的参数列表中都不用包括self。
演示一个类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | wash.py
class Washer:
def __init__(self):
self.water = 0
self.scour = 0
def add_water(self,water):
print ( 'Add water:' ,water)
self.water = water
def add_scour(self,scour):
self.scour = scour
print ( 'Add scour:' ,self.scour)
def start_wash(self):
print ( 'Start wash...' )
if __name__ == '__main__' :
w = Washer()
w.add_water(10)
w.add_scour(2)
w.start_wash()
|
运行结果:

更多Python相关技术文章,请访问Python教程栏目进行学习!
以上就是python中如何调用类的方法的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
比较Python和ruby
Python中如何创建字典
Python在每个字符后添加空格的实例
sqlalchemy的实例介绍
Python 中 / 与 // 的区别
int是Python的保留字吗
Python库怎么安装
Python人工智能是什么意思
Python update函数定义及作用实例解析
学习Python安装什么
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中如何调用类的方法