python教程_小白入门2021/3/17
学习目标
P 164 类方法和静态方法
class Person(object):
type = 'human'
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food): # 对象方法有一个参数self,指的是实例对象
print(self.name + '正在吃' + food)
# 如果一个方法里没有用到实例对象的任何属性,可以将这个方法成static
@staticmethod
def demo():
print('hello')
@classmethod
def test(cls): # 如果这个函数只用到了类属性,我们可以把定义成为一个类方法
# 类方法会有一个参数 cls,也不需要手动的传参,会自动传参
# cls 指的是类对象 cls is Person
print(cls.type)
print(