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

类方法和静态方法

普通函数

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

相关推荐

  • 磁盘管理(SWAP、dd、quota、RAID、LVM)

    2016-08-26: 授课内容: 1、SWAP交换分区的创建 2、dd命令的使用 3、设定文件系统配额 4、设定和管理软RAID设备 5、配置逻辑卷、逻辑卷快照 1、swap (1)SWAP分区:模拟内存,当物理内存不足时,进程需要内存资源是,内存会把一部分没有在用的进程分页挪到硬盘的模拟内存中,腾出空间被现在需要使用内存资源的进程 即其作用是可以允许内存…

    Linux干货 2016-09-01
  • 博客作业-N22第二周

    1、linux上的文件管理类命令都有那些,其常用的使用方法及其相关示例演示。 答: cp 复制文件 [root@localhost network-scripts]# cp ifcfg-eno16777736 ifcfg-eno.bak [root@localhost network-scripts]# ls ifcfg-eno16777736  …

    Linux干货 2016-08-22
  • 文件管理基础知识及命令详解

    文件系统     文件和目录被组织成一个单根倒置树结构     文件系统从根目录下开始,用“ /”表示     根文件系统(rootfs): root filesystem    …

    Linux干货 2016-08-05
  • linux的内建命令和外部命令

    摘要:    Linux命令有内部命令(内建命令)和外部命令之分,内部命令和外部命令功能基本相同,但是其工作机制相差很大。本文就内建命令和外部命令做一下介绍。 一、内部命令(内建命令)    内部命令,实际上是shell程序的一部分,其中包含的是一些比较简单的linux系统命令,这些命令由shell程序识别并在shel…

    Linux干货 2016-10-18
  • 第七周作业

    查看作业内容请移步此链接:http://www.cnblogs.com/wangenzhi/p/6403568.html

    Linux干货 2017-02-15
  • nfs和samba服务

    nfs:      nfs: Network File System                      #网络文件系统      nis:Network Information Se…

    Linux干货 2017-04-27