python类中怎么定义方法
对于Python类中,方法的定义方式,我们可以归纳有4种类型:推荐学习:Python视频教程)
1、不带self、cls参数且不加装饰器(staticmethod、classmethod)
定义代码如下:
class Student(object): def func(name): print('my name is {}'.format(name))
2、正常的方法定义,带self参数
定义代码如下:
class Student(object): def func(self, name): print('my name is {}'.format(name))
3、类方法:加装饰器(classmethod)
定义代码如下:
class Student(object): @classmethod def func(cls, name): print('my name is {} from {}'.format(name, cls.__name__))
静态方法:加装饰器(staticmethod)
定义代码如下:
class Student(object): @staticmethod def func(name): print('my name is {}'.format(name))
更多Python相关技术文章,请访问Python教程栏目进行学习!
以上就是python类中怎么定义方法的详细内容,更多请关注教程集其它相关文章!
您可能感兴趣的文章:
python类中怎么定义方法
python类怎么定义全局变量
python怎么使用模块中的类
python定义的类怎么用
彻底搞懂Python中的类
Python类调用实例方法
python入门必读class定义类
linux怎么执行python脚本
python中的map怎么使用(方法详解)
为什么大数据用python