python类函数,静态函数,属性

# -*- coding:utf-8 -*-

# 类函数 和 静态函数

class People(object):
    __seats = 10  # 总计座位数
    __seat_type = 1  # 座位类型

    def __init__(self, name, age):
        # 调用父类的初始化函数
        super(People, self).__init__()
        # 初始化当前类对象的一些属性
        self.name = name
        self.age = age

    @property
    def seats(self):  # 商品总价
        return self.__seats

    @classmethod
    def change_seats(cls, n, t):
        People.__seats = n
        cls.__seat_type = t
        print(f'总共{cls.__seats} seats')

    # 对象函数,只能由对象调用
    def eat(self):
        print(f'{self.name}该吃饭了...')

    # 类函数
    # 装饰器是以@开头,@结构的称之为语法糖,装饰器的作用主要是给一些现有的函数增加一些额外的功能
    @classmethod
    def work(cls, time):
        # cls  class 如果是类调用该函数,cls指的就是这个类
        # 如果是对象调用该函数,cls指的就是这个对象的类型
        print(cls)
        print(time)

    @classmethod
    def sleep(cls):
        print('每一个类函数前都需要添加装饰器')

    # 静态函数
    # @staticmethod 描述的函数称为静态函数,静态函数可以由类和对象调用,函数中没有隐形参数
    @staticmethod
    def run(time):
        print('跑步%s分钟' % time)

    def take_(self, n):
        People.__seats = People.__seats - n
        print(f'{self.name} take {n} seats')

    def return_(self, n):
        People.__seats = People.__seats + n
        print(f'{self.name} return {n} seats')


# 对象函数只能由对象调用
# 类函数由类调用,也可以用对象调用
People.work(10)
p1 = People('张三', 22)
p1.work(20)

People.run(10)
p1.run(20)

print(p1.seats)  # 属性,不需要加括号
People.change_seats(20, 2)
print('现有座位:', p1.seats)

p1.take_(2)
p1.return_(1)
print('剩余座位:', p1.seats)

p2 = People('李四', 33)
p2.take_(5)
print('剩余座位:', p1.seats)


输出:

<class '__main__.People'>
10
<class '__main__.People'>
20
跑步10分钟
跑步20分钟
10
总共20 seats
现有座位: 20
张三 take 2 seats
张三 return 1 seats
剩余座位: 19
李四 take 5 seats
剩余座位: 14
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值