面向对象的实例方法,类方法和静态方法

类方法和静态方法

普通函数

class Person:
    def normal_method():
        print('normal')

Person.normal_method()
# Person().normal_method()

print(Person.__dict__)

5a0126547e36a94c94000001

  • Person.normal_method()可以,是因为这个方法只是被Person这个名词空间管理的一个普通方法而已,normal_method()只是Person的一个属性而已
  • Person().normal_method()不可以,由于normal_method()在定义的时候没有指定self,所以不能完成实例对象的绑定,所以不能用
  • 虽然语法是对的,但是禁止这么写

实例方法

class Person:
    def method(self):
        return("{}'s method".format(self))

tom = Person()
print(tom.method())

5a014bb87e36a94c94000006


类方法

class Person:
    @classmethod
    def class_method(cls):
        print('class = {0.__name__} ({0})'.format(cls))
        cls.HEIGHT = 179

Person.class_method()
print(Person.__dict__)

5a0128ae7e36a94c94000002

  • 在类定义中,使用@classmethod装饰器修饰的方法
  • 必须至少有一个参数,且第一个参数留给clscls指代调用者即类对象自身
  • 不要修改cls标识符
  • 通过cls可以直接操作类的属性
  • python的类方法,类似于C++、Java中的静态方法

静态方法

class Person:
    @classmethod
    def class_method(cls):
        print('class = {0.__name__} ({0})'.format(cls))
        cls.HEIGHT = 179

    @staticmethod
    def static_method():
        print(Person.HEIGHT)

Person.class_method()
Person.static_method()
print(Person.__dict__)

5a012e537e36a94c94000003

  • 在类定义中,使用@staticmethod装饰器修饰的方法
  • 调用时,不会隐式地传入参数
  • 静态方法,只是表明这个方法属于这个名词空间

方法的调用

class Person:
    def normal_method():
        return('normal')

    def method(self):
        return("{}'s method".format(self))

    @classmethod
    def class_method(cls):
        cls.HEIGHT = 179
        return ('class = {0.__name__} ({0})'.format(cls))

    @staticmethod
    def static_method():
        return (Person.HEIGHT)

print('----类访问-----')
print(1, Person.normal_method())
# print(2, Person.method())    # 报错,因为没有实例化,缺少一个参数,给一个参数就行
print(3, Person.class_method())
print(4, Person.static_method())
print(Person.__dict__, end='\n\n')

print('------实例访问-------')
print('----tom----')
tom = Person()
# print(1,tom.normal_method())   # 报错,多给了self参数
print(2, tom.method())
print(3, tom.class_method())
print(4, tom.static_method(), end='\n\n')

print('----jerry----')
jerry = Person()
# print(1, jerry.normal_method())
print(2, jerry.method())
print(3, jerry.class_method())
print(4, jerry.static_method())

5a0147537e36a94c94000004
5a01476d7e36a94c94000005


总结

  • 类除了实例方法,都可以调用,实例方法需要对象的实例作为第一参数
  • 实例除了普通方法,都可以调用,调用实例方法传入实例自身,静态方法类方法需要传入实例的类

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/88299

(0)
nolannolan
上一篇 2017-11-11 16:04
下一篇 2017-11-12 11:23

相关推荐

  • 马哥教育网络班21期-第7周课程练习

    第7周课程练习 创建一个10G分区,并格式为ext4文件系统; 添加一块硬盘sdb 要求其block大小为2048,       预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; # mke2fs -t ext4 -b 2048 -L 'MYDATA' -m 2 # mount -o ac…

    Linux干货 2016-10-09
  • ​Bash2

    字串比较时变量最好使用"" 这样就不会报错了,只是退出码不为0 组合条件:     与:[ condition1 -a condition2 ]或condition1 && condition2     或:[ condition1 -o co…

    Linux干货 2016-09-25
  • 第二周作业新

    一、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 pwd :显示当前所在路径   cd :切换目录   cd – :在上次目录与当前目录之间切换。   cd ~ :切换回自己的家目录   cd . :当前目录      cd .. :切换到…

    2017-02-18
  • vncserver安装配置小结

      安装VNC Server 端:        1. yum install  tigervnc-server         //RHEL6: vnc server的包名          yum &nbs…

    Linux干货 2016-03-09
  • MariaDB安装与配置

    MariaDB安装与配置  本文是基于CentOS7.2系统来进行mariadb的安装与配置,安装前请关闭selinux和在iptables规则中开放3306端口,在此次我们直接清空了iptables规则。 Iptables –F vim /etc/selinux/config #SELINUX=enforcing ##注释掉此项## #SELIN…

    Linux干货 2017-02-18
  • Linux系统启动过程及其修复过程简析

    Linux组成 Linux: kernel+rootfs     kernel: 进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能     rootfs:程序和glibc     库:函数集合, function, 调用接口(头文…

    Linux干货 2016-09-19