#!/usr/bin/env python # -*- coding: utf-8 -*- # 指定源碼文件編碼,python默認爲ASCII __metaclass__ = type # 肯定使用新式類 class Person: def setName(self, name): # self 爲是當前實例的引用 self.name = name def getName(self): return self.name def greet(self): print "hello, world! I'm %s." % self.name def __inaccessible(self): # 私有函數,外界不能夠訪問 print "Bet you can't see me..." def _inaccessible(self): # 不會被import導出 print "Bet you can't see me..." foo = Person() # 建立對象 bar = Person() foo.setName("foo") bar.setName("bar") print foo.getName() print bar.getName() foo.greet() bar.greet() #foo.__inaccessible() foo._inaccessible() print "------------------------" ''' ''' class MemberCounter: print "class MemberCounter begin defined" members = 0 members2 = 0 def init(self): MemberCounter.members += 1 MemberCounter.members2 += 1 m1 = MemberCounter() m1.init() m2 = MemberCounter() m2.init() print "----" print "m1.members", m1.members print "m2.members", m2.members print "MemberCounter.members", MemberCounter.members print "----" print "modify m1.members" m1.members='m1' print "----" print "m1.members", m1.members print "m2.members", m2.members print "MemberCounter.members", MemberCounter.members print "----" print "modify MemberCounter.members" MemberCounter.members = "MemberCounter" print "----" print "m1.members", m1.members print "m2.members", m2.members print "MemberCounter.members", MemberCounter.members print "----" print "create m3" m3=MemberCounter() print "----" print "m1.members", m1.members, "m1.members2", m1.members2 print "m2.members", m2.members, "m2.members2", m2.members2 print "m3.members", m3.members, "m3.members2", m3.members2 print "MemberCounter.members", MemberCounter.members, "MemberCounter.members2", MemberCounter.members2 print "----" print "modify MemberCounter.members2" MemberCounter.members2= "members2" print "----" print "m1.members", m1.members, "m1.members2", m1.members2 print "m2.members", m2.members, "m2.members2", m2.members2 print "m3.members", m3.members, "m3.members2", m3.members2 print "MemberCounter.members", MemberCounter.members, "MemberCounter.members2", MemberCounter.members2 print "----" m3.members2="m3.members2" MemberCounter.members2= "members2 last" print "----" print "m1.members", m1.members, "m1.members2", m1.members2 print "m2.members", m2.members, "m2.members2", m2.members2 print "m3.members", m3.members, "m3.members2", m3.members2 print "MemberCounter.members", MemberCounter.members, "MemberCounter.members2", MemberCounter.members2 print "----" ''' ''' print "---------------------" class Filter: def init(self): self.blocked = [] # 建立成員變量 def filter(self, sequence): return [x for x in sequence if x not in self.blocked] class SPAMFilter(Filter): # 繼承,能夠是逗號分隔的多個基類 def init(self): # 重載 self.blocked = ['SPAM'] f = Filter() f.init() print f.filter([1,2,3]) s = SPAMFilter() s.init() print s.filter(["SPAM", "SPAM","SPAM","SPAM", "eggs", "bacom" ]) print "---------------------" class C: def dis(self): self.c=1 print self.c c=C() c.dis() c.cc=3 print "c.cc", c.cc print "c.__dict__", c.__dict__ print "C.__dict__", C.__dict__ print "Filter.__dict__", Filter.__dict__ print "f.__dict__", f.__dict__ print "s.__dict__", s.__dict__ print "m1.__dict__", m1.__dict__ print "m2.__dict__", m2.__dict__ print "m3.__dict__", m3.__dict__ print "MemberCounter.__dict__", MemberCounter.__dict__