xone

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

class Test(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age

t = Test("老王",18)
print(t.name)

#动态添加属性
t.addr = "山东"
print(t.addr)

#动态添加实例方法
def info1(self):
    print("---info1---")
import types
t.info1 = types.MethodType(info1,t)
t.info1()

#动态添加静态方法
@staticmethod
def info2():
    print("---info2---")
Test.info2 = info2
Test.info2()

#动态添加类方法
@classmethod
def info3(cls):
    print("---info3---")
Test.info3 = info3
Test.info3()

  输出

老王
山东
---info1---
---info2---
---info3---

  

 

posted on 2019-01-15 13:35  周小百  阅读(169)  评论(0编辑  收藏  举报