python 实例属性、类属性、实例方法、类方法、静态方法

class People():
    peoCount = 0  # 类属性
    def __init__(self,name,age):
        # 实例属性
        self.__name = name
        self.__age = age
        People.peoCount += 1

    def getProperty(self):  # 定义实例方法
        return self.__dict__

    @classmethod  #定义类方法
    def getPeoCount(cls):
        return cls.peoCount

    @staticmethod  # 定义静态方法
    def peoInvalid(**kwargs):
        print(kwargs)
        if kwargs["age"] < 18:
            return False
        else:
            return True

peo = People("张三",22)
peo1 = People("李四",23)
print(peo.getProperty())
print(People.getPeoCount())
peoInfo = {'name':'王五','age':17}
People.peoInvalid(**peoInfo)

 

posted @ 2024-03-31 15:43  远洪  阅读(9)  评论(0编辑  收藏  举报