#!/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__