首页 > 编程语言 > 详细

Python面向对象编程 绑定方法

时间:2018-01-23 15:08:16      阅读:207      评论:0      收藏:0      [点我收藏+]
类的函数属性绑定给不同对象,是不同的绑定方法。对象调用绑定方法时会把对象本身当做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面向对象编程 绑定方法

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!