网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 python的类方法和静态方法
内容
    本文实例讲述了python的类方法和静态方法。分享给大家供大家参考。具体分析如下:
    python没有和C++中static关键字,它的静态方法是怎样的呢?还有其它语言中少有的类方法又是神马?
    python中实现静态方法和类方法都是依赖于python的修饰器来实现的。
    复制代码 代码如下:class MyClass:
    def method(self):
    print("method")
    @staticmethod
    def staticMethod():
    print("static method")
    @classmethod
    def classMethod(cls):
    print("class method")
    大家注意到普通的对象方法、类方法和静态方法的去别了吗?
    对象方法有self参数,类方法有cls参数,静态方法是不需要这些附加参数的。
    在C++中是没有类方法着个概念的的
    代码如下:
    class A(object):
    "This ia A Class"
    @staticmethod
    def Foo1():
    print("Call static method foo1()\n")
    @classmethod
    def Foo2(cls):
    print("Call class method foo2()")
    print("cls.__name__ is ",cls.__name__)
    A.Foo1();
    A.Foo2();
    结果是:
    Call static method foo1()
    Call class method foo2()
    cls.__name__ is A
    希望本文所述对大家的Python程序设计有所帮助。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/23 8:38:22