码迷,mamicode.com
首页 > 编程语言 > 详细

Python面向对象编程 绑定方法

时间:2018-01-23 15:44:11      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:post   fun   enc   ini   body   不同   nan   color   blog   

类的函数属性绑定给不同对象,是不同的绑定方法。对象调用绑定方法时会把对象本身当做self参数传入
示例代码
class People:
    country = China
?
    def __init__(self, name, sex, age):
        self.Name = name
        self.Sex = sex
        self.Age = age
?
    def test(self):
        print(self)
?
?
conan = People(Conan, male, 8)
wukong = People(wukong, male, 18)
?
wukong.test()  # People.test(wukong)
conan.test()   # People.test(conan)
?
print(People.test,People.test(123))
print(wukong.test,wukong.test())
print(conan.test,wukong.test())

打印结果:

# <__main__.People object at 0x10813a320>
# <__main__.People object at 0x10813a2e8>
# <function People.test at 0x1081291e0>
# <bound method People.test of <__main__.People object at 0x10813a320>>
# <bound method People.test of <__main__.People object at 0x10813a2e8>>

 

 

 

Python面向对象编程 绑定方法

标签:post   fun   enc   ini   body   不同   nan   color   blog   

原文地址:https://www.cnblogs.com/Bacardi/p/8335688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!