首页 > 代码库 > 【python学习笔记】7.更加抽象
【python学习笔记】7.更加抽象
【python学习笔记】7.更加抽象
- 类的定义就是执行代码块
- 在内存保存一个原始实例,可以通过类名来访问
- 类的实例化,是创建一个原始实例的副本, 并且所有成员变量与原始实例绑定
- 通过修改实例变量,可以解除与原始实例的绑定
- self表示当前实例的引用
- 成员变量也称为特性
- __bases__: 基类
- __class__: 对象类型
- __dict__: 所有特性
- python的接口不用显式的制定对象必须包含哪些方法,只要对象符合当前接口就可以调用
- 可以对象上通过赋值的方式,创建变量
#!/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 += 1m1 = MemberCounter()m1.init()m2 = MemberCounter()m2.init()print "----"print "m1.members", m1.membersprint "m2.members", m2.membersprint "MemberCounter.members", MemberCounter.membersprint "----"print "modify m1.members"m1.members=‘m1‘print "----"print "m1.members", m1.membersprint "m2.members", m2.membersprint "MemberCounter.members", MemberCounter.membersprint "----"print "modify MemberCounter.members"MemberCounter.members = "MemberCounter"print "----"print "m1.members", m1.membersprint "m2.members", m2.membersprint "MemberCounter.members", MemberCounter.membersprint "----"print "create m3"m3=MemberCounter()print "----"print "m1.members", m1.members, "m1.members2", m1.members2print "m2.members", m2.members, "m2.members2", m2.members2print "m3.members", m3.members, "m3.members2", m3.members2print "MemberCounter.members", MemberCounter.members, "MemberCounter.members2", MemberCounter.members2print "----"print "modify MemberCounter.members2"MemberCounter.members2= "members2"print "----"print "m1.members", m1.members, "m1.members2", m1.members2print "m2.members", m2.members, "m2.members2", m2.members2print "m3.members", m3.members, "m3.members2", m3.members2print "MemberCounter.members", MemberCounter.members, "MemberCounter.members2", MemberCounter.members2print "----"m3.members2="m3.members2"MemberCounter.members2= "members2 last"print "----"print "m1.members", m1.members, "m1.members2", m1.members2print "m2.members", m2.members, "m2.members2", m2.members2print "m3.members", m3.members, "m3.members2", m3.members2print "MemberCounter.members", MemberCounter.members, "MemberCounter.members2", MemberCounter.members2print "----"‘‘‘‘‘‘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.cc=C()c.dis()c.cc=3print "c.cc", c.ccprint "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__
【python学习笔记】7.更加抽象
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。