美文网首页
python基础 -- 静态方法和类方法

python基础 -- 静态方法和类方法

作者: fada492daf5b | 来源:发表于2018-01-29 20:20 被阅读0次

1. 作用

一般直接使用类来调用方法

2. 操作

# 静态方法
# staticmethod

class Hero(object):
    def __init__(self, name):
        self.name = name
    
    def set_name(self, name):
        self.name = name

    @staticmethod
    def move(action):
        print('{}......'.format(action))

    @classmethod
    def say(cls, words):
        print('{} say {}...'.format(cls, words))

superman = Hero('Mario')
superman.set_name('Tommy')
superman.move('flying')
Hero.move('running')
Hero.say('go go go...')

相关文章

网友评论

      本文标题:python基础 -- 静态方法和类方法

      本文链接:https://www.haomeiwen.com/subject/xgyfzxtx.html