python学习笔记7.更加抽象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 += 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__
以上是关于python学习笔记7.更加抽象的主要内容,如果未能解决你的问题,请参考以下文章