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

类方法和静态方法

普通函数

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装饰器修饰的方法
  • 必须至少有一个参数,且第一个参数留给cls,cls指代调用者即类对象自身
  • 不要修改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

相关推荐

  • linux基础知识之nmcli

           CentOS7才有的功能网络组:将多块网卡聚合在一起的方法,从而实现冗错和提高吞吐量        网络组不同于旧版中的bonding技术,提供了更好的性能和扩展性        网路组由内核驱动和teamd守护进程实现 &n…

    Linux干货 2016-09-07
  • 第六周作业

    请详细总结vim的使用 模式转换 编辑模式:默认模式     编辑模式–>输入模式         i:insert,在光标所在处插入         …

    Linux干货 2017-02-15
  • 22期第九周课堂作业

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash declare -i a=0 declare -i b=0 n=`cat /etc/passwd |cu…

    Linux干货 2016-10-17
  • 运维第一步

    学友分享各自工作经历

    Linux干货 2018-03-26
  • shell脚本编程之变量

    一、创建shell脚本文件   创建shell脚本时,必须在第一行指定要使用的shell。格式为:   #!/bin/bash   通常在shell脚本中,#代表注释行,即不被执行的行,而第一行“#!”则是表示用哪个shell来执行脚本。   实例:   让我们来执行一下这个脚本:   上面的结果显…

    Linux干货 2016-02-14
  • Python

    Python中os和shutil模块
    csv文件和ini文件简介

    Linux干货 2017-10-30