python中如何调用类的方法-Python教程

资源魔 27 0

类的办法的挪用:

与一般的函数挪用相似

一、类的外部挪用:self.<办法名>(参数列表)。

二、正在类的内部挪用:<实例名>.<办法名>(参数列表)。

留意:以上两种挪用办法中,提供的参数列表中都不必包罗self。

演示一个类:

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()

运转后果:

1.jpg

更多Python相干技巧文章,请拜访Python教程栏目进行学习!

以上就是python中若何挪用类的办法的具体内容,更多请存眷资源魔其它相干文章!

标签: python教程 python编程 python使用问题 python中如何调用类的方法

抱歉,评论功能暂时关闭!