Google

星期一, 八月 27, 2007

Python中类的一些实现办法

http://bbs.chinaunix.net/thread-977343-1-1.html
看来是跟C里实现面象对象的方式差不多啊。

In [1]: def greet(someone, anotherone):
...: print someone, 'say hello to', anotherone
...:

In [2]: class Person(object):
...: def __init__(self, name):
...: self.name=name
...: def __str__(self):
...: return self.name
...:

In [3]: p1 = Person('test1')

In [4]: p2 = Person('test2')

In [5]: greet(p1, p2)
test1 say hello to test2

In [6]: Person.greet = greet

In [7]: p1.greet(p2)
test1 say hello to test2

0 条评论:

发表评论

<< 主页

辽ICP备05003652号
流风洄雪听天籁,轻云蔽日看落花

Powered by Blogger