• Python基础之类方法和静态方法


      小叙一会儿:

      通常情况下,在类中定义的所有函数(注意了,这里说的就是所有,跟self啥的没关系,self也只是一个再普通不过

    的参数而已)都是对象的绑定方法,对象在调用绑定方法时会自动将自己作为参数传递给方法的第一个参数。除此之外还

    有两种常见的方法:静态方法和类方法,二者是为类量身定制的,但是实例非要使用,也不会报错,后续将介绍。

      一、类方法:

      定义:在类里面把一个方法绑定给类,说白了类方法是给类用的,该方法由装饰器@classmethod所装饰,类.绑定

    到类的方法(),会把类本身当做第一个参数自动传给绑定到类的方法中。

    class A: #创建父类
        x = 1
        @classmethod #使用装饰器,变为类的绑定方法
        def test(cls): #定义了类的方法
            print(cls,cls.x)#拿掉一个类的内存地址后,就可以实例化或者引用类的属性了
    
    class B(A): #子类B,B中没有test类的绑定方法,就去父类中找,并将类名当第一个位置参数自动传到方法里
        x = 2
    
    B.test() #调用test类的绑定方法
    print(B.test) #查看B.test的属性
    -------------输出结果----------------
    <class '__main__.B'> 2 
    <bound method A.test of <class '__main__.B'>> #test是A的绑定方法,这里B继承了
    

      应用场景:

    class Date:
        def __init__(self,year,month,day):
            self.year = year
            self.month = month
            self.day = day
        @classmethod
        def now(cls):
            t = time.localtime()
            obj = cls(t.tm_year,t.tm_mon,t.tm_mday)
            return obj
        @classmethod
        def tomorrow(cls):
            t = time.localtime(time.time()+86400)
            obj = cls(t.tm_year,t.tm_mon,t.tm_mday)
            return obj
    
    class EuroDate(Date): #__str__,打印由这个类产生的对象时,会触发执行
        def __str__(self): #定义在类内部,必须返回一个字符串类型
            return "年:%s,月:%s,日:%s"%(self.year,self.month,self.day)
    
    e1 = EuroDate(2016,12,13) #实例化对象e1
    print(e1)
    e2 = EuroDate.now() #调用类的绑定方法,并赋值给e2
    print(e2) #打印返回结果
    e3 = EuroDate.tomorrow() #调用类的绑定方法,并赋值给e3
    print(e3) #打印返回结果
    print(EuroDate.now) #查看数据类型
    print(EuroDate.tomorrow) #查看数据类型
    ---------------输出结果---------------
    年:2016,月:12,日:13
    年:2017,月:4,日:22
    年:2017,月:4,日:23
    <bound method Date.now of <class '__main__.EuroDate'>>
    <bound method Date.tomorrow of <class '__main__.EuroDate'>>

      强调,注意注意注意:静态方法和类方法虽然是给类准备的,但是如果实例去用,也是可以用的,只不过实例去调

    用的时候容易让人混淆,不知道你要干啥。

    二、静态方法:

      定义:是一种普通函数,在类的作用域里面时,不会对任何实例类型进行操作,前面必须要加上一个装饰器

    @staticmethod,我们将这种方法就称为静态方法。

    class A:
        @staticmethod  #相当于test = staticmethod(test)
        def test(x,y,z): #把函数test做成静态方法,self和x啥的没有不同都是参数名
            print(x,y,z)
    

      静态方法与绑定方法的区别:

        1、凡是定义在类的内部,并且没有被任何装饰器修饰过的方法,都是绑定方法:有自动传值功能;

        2、但凡是定义在类的内部,并且被staticmethod装饰器修饰过的方法,都是解除绑定的方法,实际上就函数:

    就没有自动传值功能了。

      使用演示:

    print(type(A.test)) #类型本质就是函数
    A.test(1,2,3) #调用函数应该有几个参数就传几个参数
    a = A() #实例化对象a
    a.test(3,2,1) #对象a在调用test,此时test就是一普通函数,有几个形参就要传几个值
    print(type(a.test)) #类型本质就是函数
    --------------输出结果-----------------
    <class 'function'> #A.test的类型就是函数
    1 2 3 
    3 2 1
    <class 'function'> #a.test的类型就是函数
    

      应用场景:

    import time
    class Date:
        def __init__(self,year,month,day):
            self.year=year
            self.month=month
            self.day=day
        @staticmethod
        def now(): #用Date.now()的形式去产生实例,该实例用的是当前时间
            t=time.localtime() #获取结构化的时间格式
            return Date(t.tm_year,t.tm_mon,t.tm_mday) #新建实例并且返回
        @staticmethod
        def tomorrow():#用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
            t=time.localtime(time.time()+86400)
            return Date(t.tm_year,t.tm_mon,t.tm_mday) #新建实例并且返回
    
    a=Date('2008',8,18) #实例化对象,自己定义时间
    b=Date.now() #采用当前时间
    c=Date.tomorrow() #采用明天的时间
    
    print(a.year,a.month,a.day) #打印实例化对象的时间,年,月,日
    print(b.year,b.month,b.day)
    print(c.year,c.month,c.day)
    ----------------输出结果---------------------
    2008 8 18
    2017 4 21
    2017 4 22
    

      注意:计算机是不知道年、月、日,这个概念的,计算机只知道秒。so,一天的时间,都得换算成秒。

    one_day = 24(小时) * 60(分) * 60(秒) =86400(秒)。

      

  • 相关阅读:
    除了Web和Node,JavaScript还能做什么
    从Hybrid到React-Native: JS在移动端的南征北战史
    当React开发者初次走进React-Native的世界
    【React-Native】React-Native组件样式合集
    【github】论怎么去写一个高大上的ReadMe
    https://github.com/sindresorhus/awesome-nodejs 清单
    https://github.com/akullpp/awesome-java 清单
    https://github.com/ChristosChristofidis/awesome-deep-learning 清单
    https://github.com/josephmisiti/awesome-machine-learning 清单
    SpringBoot 为什么能够自动的注入一些常用的Bean ?详细分析SpringBoot 自动配置的实现
  • 原文地址:https://www.cnblogs.com/Michael--chen/p/6745376.html
Copyright © 2020-2023  润新知