您的位置:首页 > 编程语言 > Python开发

python入门:静态方法、类成员方法

2018-03-22 10:04 225 查看
若有帮助到你,记得点赞哦!

参考:python基础教程第二版 Hetland
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 22 09:52:45 2018

@author: Lelouch_C.C
"""

#"""
class MyClass:
def smeth():
print('lalala...')
smeth=staticmethod(smeth) #静态方法的定义没有self参数,且能够被类本身直接调用

def cmeth(cls):
print('hahaha...')
cmeth=classmethod(cmeth) #类成员方法定义了cls,类似于self,类成员方法可以直接用类的具体对象调用
print(MyClass.smeth())
print(MyClass.cmeth())

#"""
"""
class MyClass:
@staticmethod
# @操作符在方法或函数上方将装饰器列出,可指定多个装饰器,多个装饰器在应用时的顺序与指定顺序相反
def smeth():
print('lalala...')
#静态方法
@classmethod
def cmeth(cls):
print('hahaha...')
#类成员方法

print(MyClass.smeth())
print(MyClass.cmeth())

#"""
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: