1.抽象类设计python
## abs.py
## 第一种形式,伪抽象类,
class Super(object):
def delegate(self):
self.action() ## action具体实现方法由子类定义z
def action(self):
raise NotImplementedError('action must be defined!')
class Sub(Super):
def action(self):
print("call sub action ...")
## 第二种形式
## py3.x
from abc import abstractmethod,ABCMeta
class Super(metaclass=ABCMeta): ## 定义为抽象类
def delegate(self):
self.action()
@abstractmethod
def action(self):
pass
class Sub(Super):
def action(self):
print("call sub action ...")
## py2.x
from abc import abstractmethod,ABCMeta
class Super:
__metaclass__ = ABCMeta ## 经过类属性声明
def delegate(self):
self.action()
@abstractmethod
def action(self):
pass
class Sub(Super):
def action(self):
print("call sub action ...")
## 第二种形式的Super不可以进行实例化,第一种形式能够进行实例化复制代码
2.类的继承与组合设计数据库
## task.py
## 继承
class Employee:
def __init__(self, name, age):
self.__name = name ## 定义私有属性,严格意义上是属于伪属性
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def work(self):
print("employ work ...")
class Engineer(Employee):
def work(self):
print("engineer coding ...")
class ProductManager(Employee):
def work(self):
print("product manager desgin ...")
## 定义组合
class Customer:
def __init__(self, name):
self.__name = name
def post_requirement(self):
print("the %s post requirement ..." % self.__name)
class ProjectGroup:
def __init__(self, engineer, manager):
self.__engineer = engineer
self.__manager = manager
def build_dtrees(self, customer_name):
customer = Customer(customer_name)
customer.post_requirement()
self.__manager.work()
self.__engineer.work()
>>> engineer = Engineer("keithl",27)
>>> manager = ProductManager("keithl",27)
>>> pg = ProjectGroup(engineer,manager)
>>> pg.build_dtrees("xiaoming")
the xiaoming post requirement ...
product manager desgin ...
engineer coding ...复制代码
3.类的委托(代理)python3.x
__getattr__
内置方法并使用getattr
方法来回调被包装类对象的属性方法## wrapper.py
class Wrapper:
def __init__(self,object):
self.__wrapper = object ## 传递被包装的对象
def __getattr__(self,attrname):
print("call wrapper for attr[%s] ...." % attrname)
return getattr(self.__wrapper,attrname)
def __str__(self):
return str(self.__wrapper)
>>> engineer = Engineer("keithl",27)
>>> we = Wrapper(engineer)
>>> we.work("dtrees")
call wrapper for attr[work] ....
engineer review and coding ...复制代码
4.伪属性bash
__
双下划线,后面不添加下划线,py会将这类属性转换为_className__attrName
_className__attrName
定义Person类私有的name属性app
## private.py
class Person:
__template_name = "person instance template name"
def __init__(self,name):
self.__name = name ## __name 属于Person类,
def get_name(self):
return self.__name
@staticmethod
def get_template_name():
return Person.__template_name
>>> p = Person("keithl")
>>> print(p.get_name())
keithl
>>> print(p._Person__name)
keithl
>>> print(p.__name)
AttributeError: 'Person' object has no attribute '__name'
>>> print(dir(p))复制代码
定义Person的子类各自的私有属性函数
## sub.py
class Manager(Person):
def __init__(self,name):
self.__name = name ## __name 属于Manager类
class Engineer(Person):
def __init__(self,name):
self.__name = name ## __name 属于Engineer类
>>> m = Manager("manager")
>>> e = Engineer("engineer")
## 打印的结果以下图所示
>>> print(dir(m))
>>> print(dir(e))复制代码
5.方法的绑定与未绑定post
## fn.py
class BoundClass:
def action(self):
print("bound class action ....")
""" python3.x未绑定self对象的方法均是类定义的函数,注意这个还不是属于静态方法和类方法 """
def unbound(num): ## work in py3.x,fail in py2.x
print("the number is %d" % num)
>>> t = BoundClass()
>>> t.action() ## action方法绑定实例对象self,能够直接经过点号运算调用
>>> m = BoundClass.action ## m是没有绑定实例对象self的方法
>>> m(t) ## 调用须要传递实例对象t复制代码
6.对象工厂方法ui
## fa.py
def factory(aClass,*pargs,**kwargs): ## aClass 具体是什么类不清楚,只有在运行的时候才知道
return aClass(pargs,kwargs)
## run-fa.py
""" 如今有一个配置文件setting.py,主要是用做配置数据库的一系列参数,其中须要指定一个类是代表使用哪一种数据库,如MySQL、Oracle... 假设在一个db.py定义了实现不一样的数据库对应的类模板 """
import setting.py ## 读取配置文件
classargs = setting_db_config ## 获取实例化链接对象的数据库配置参数,uri,port,username,password
classname = setting_db_class ## 获取类名称的字符串
aClass = getattr(db,classname) ## 从模块中获取类对象
connection = factory(aClass,classargs) ## 传递参数建立类实例对象复制代码