1 __metaclass__ = type # 确定使用新式类
2
3 class Person:
4
5 def setName(self, name):
6 self.name = name
7
8 def getName(self):
9 return self.name
10
11 def greet(self):
12 print("Hello, world! I'm %s." % self.name)
13
14 def __foo(self):
15 print("FOO", self.name)
16 def _bar(self):
17 print("BAR", self.name)
18
19
20 foo = Person()
21 bar = Person()
22 foo.setName('Luke Skywalker')
23 bar.setName('Anakin Skywalker')
24 foo.greet()
25 bar.greet()
26 Person.greet(bar)
27
28 # foo.__foo()
29 foo._Person__foo()
30 foo._bar()
31 # foo._Person_bar()
输出:
>>>
Hello, world! I'm Luke Skywalker.
Hello, world! I'm Anakin Skywalker.
Hello, world! I'm Anakin Skywalker.
FOO Luke Skywalker
BAR Luke Skywalker
posted on 2013-05-18 16:31
unixfy 阅读(342)
评论(0) 编辑 收藏 引用