TC_DHL 发表于 2021-5-8 09:01:32

[记录下学习心得] | 第040讲:类和对象:一些相关的BIF

本帖最后由 TC_DHL 于 2022-6-1 00:23 编辑

修饰符: classmethod | staticmethod | property

如果有不对的地方,欢迎指正, 互相学习哈!


修饰符:类方法 @classmethod | 无需显式地传递类名做实参
class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 设定修饰符@类方法 | 类的函数或者叫类的方法output_modules
    @classmethod
    def output_modules(cls):
      for (i,s) in cls.__modules.items():
            print(i, ':', s)

# 调用类的方法output_modules,无需显式地传递类名做实参
Computer.output_modules()

#-------------------------------------------------------------
# 输出结果:
# cpu : Intel
# 内存 : 镁光
# 硬盘 : 970-Pro
也可被其他类直接进行调用(感觉有点全局的意思), 看例子代码如下:
class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 设定修饰符@类方法 | 类的函数或者叫类的方法output_modules
    @classmethod
    def output_modules(cls):
      for (i,s) in cls.__modules.items():
            print(i, ':', s)


class OtherClass:
    def __init__(self):
      pass
    def _test_OtherClass(self):
      # 调用类的方法output_modules,无需显式地传递类名做实参
      Computer.output_modules()

aaaa = OtherClass()
aaaa._test_OtherClass()

#-------------------------------------------------------------
# 输出结果:
# cpu : Intel
# 内存 : 镁光
# 硬盘 : 970-Pro

修饰符:静态方法 @staticmethod | 必须显式地传递类名做实参
class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 在静态方法search_module中定义形参var,准备传递类:Computer
    # 调用时必须显性地传递类名,才能实现类方法一样的效果
    # 设定修饰符@静态方法 | 类的函数或者叫类的方法search_module
    @staticmethod
    def search_module(var, module_value):
      print(var.__modules)

Computer.search_module(Computer, "cpu")
Computer.search_module(Computer, "内存")
Computer.search_module(Computer, "硬盘")

#-------------------------------------------------------------
# 输出结果:
# Intel
# 镁光
# 970-Pro
也可被其他类直接进行调用(有点全局的意思.....), 看例子代码如下:
class Computer:
    # 类属性modules
    __modules = {"cpu":"Intel", "内存":"镁光", "硬盘":"970-Pro"}

    # 在静态方法search_module中定义形参var,准备传递类:Computer
    # 调用时必须显性地传递类名,才能实现类方法一样的效果
    # 设定修饰符@静态方法 | 类的函数或者叫类的方法search_module
    @staticmethod
    def search_module(var, module_value):
      print(var.__modules)


class OtherClass:
    def __init__(self):
      pass

    def _test_OtherClass(self):
      # 调用类的静态方法search_module,必须显式地传递类名做实参
      Computer.search_module(Computer, "cpu")
      Computer.search_module(Computer, "内存")
      Computer.search_module(Computer, "硬盘")

aaaa = OtherClass()
aaaa._test_OtherClass()

#-------------------------------------------------------------
# 输出结果:
# Intel
# 镁光
# 970-Pro


@property 此修饰符可赋值给变量, 语法为:x = property(getx, setx, delx)
        如果是以此种方法的话, 函数名或者说是方法名可以不相同
如果是以装饰器形式使用的话, 函数名或者说是方法名必须相同, 例子代码如下:
**** Hidden Message *****

2022-06-01 推翻例子中"删除字典中内容, 这里没办法通过@modules_property.deleter以达到删除字典中某个键值"这句话:
**** Hidden Message *****

hornwong 发表于 2021-5-8 10:21:58

感谢分享!

ridiculum 发表于 2021-6-16 11:25:28

感谢分享

1835575828 发表于 2021-7-3 16:37:33

sunwenwu123 发表于 2021-7-4 15:42:38

学习{:10_281:}

鸬鹚鸟 发表于 2021-7-25 12:38:59

楼主继续加油

临时号 发表于 2021-7-25 13:11:22

感谢分享

小超超超 发表于 2021-7-26 02:58:06

看看

鸬鹚鸟 发表于 2021-7-29 09:56:43

感谢您的无私奉献

枫叶向上_ 发表于 2021-7-29 15:44:24

写挺好!

鸬鹚鸟 发表于 2021-7-31 07:34:18


感谢分享!

harry888 发表于 2022-3-9 16:28:41

XIEXIE

hornwong 发表于 2022-3-9 18:23:03

{:5_95:}

kerln888 发表于 2022-3-10 08:46:27

学习了

Treasure_00 发表于 2022-3-10 10:17:29

学习

Violet01 发表于 2022-3-10 10:55:15

学习

1271425661 发表于 2022-3-11 10:50:24

{:10_254:}

tommyyu 发表于 2022-3-12 21:20:42

{:10_277:}

tuza1205 发表于 2022-3-12 21:27:24

1

神说我是救赎 发表于 2022-3-13 11:36:09

感谢分享
页: [1] 2
查看完整版本: [记录下学习心得] | 第040讲:类和对象:一些相关的BIF