1. 程式人生 > >類補充

類補充

exc exception str lsp relation 創建 method utf inf

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 
  4 ###########接口######################
  5 # class Iorderddd():
  6 #     def fetch_one_by(self,nid):
  7 #         raise Exception(‘子類必須實現該方法‘)
  8 #
  9 # class aaa(Iorderddd):
 10 #     def fetch_one_by(self, nid):
 11 #         print(nid)
 12
# aa = aaa() 13 # aa.fetch_one_by(1) 14 15 ###########抽象類,抽象方法###################### 16 # import abc 17 # class Foo(metaclass=abc.ABCMeta): 18 # def f1(self): 19 # print(‘123‘) 20 # 21 # @abc.abstractmethod 22 # def f2(self): 23 # ‘‘‘ 24 # 25 # :return:
26 # ‘‘‘ 27 # 28 # class Bar(Foo): 29 # def f2(self): 30 # print(‘3333‘) 31 # 32 # b = Bar() 33 # b.f2() 34 ###########組合###################### 35 # class Foo: 36 # def f1(self): 37 # pass 38 # def f2(self): 39 # pass 40 # 41 # class SqlHelper:
42 # def __init__(self,f): 43 # self.f = f 44 # def fetch_one(self): 45 # self.f.f1() 46 # def fetch_all(self): 47 # pass 48 # class UserInfo: 49 # def __init__(self,helper): 50 # self.s = helper 51 # def login(self): 52 # self.s.fetch_one() 53 # def loginout(self): 54 # self.s.fetch_one() 55 # def userlist(self): 56 # self.s.fetch_all() 57 # f = Foo() 58 # b = SqlHelper(f) 59 # obj = UserInfo(b) 60 # obj.login() 61 # 類作為參數傳入到另一個類中,降低耦合 62 63 ########## 類創建的本質###################### 64 # class Foo: 65 # def __init__(self): 66 # self.name = 123 67 # def f1(self): 68 # print(‘f1‘) 69 # # 解釋器執行步驟 70 # # 1、遇到class Foo 執行Type的__init__方法 71 # # 2、type的init方法。。。 72 # obj = Foo() 73 # 3、執行type的__call__方法 74 # 執行Foo類的__new__方法 75 # 執行Foo類的__init__方法 76 77 ########## 自定義type###################### 78 # class MyType(type): 79 # def __call__(cls, *args, **kwargs): 80 # obj = cls.__new__(cls,*args,**kwargs) 81 # print(‘==========‘) 82 # obj.__init__(*args,**kwargs) 83 # return obj 84 # class Foo(metaclass=MyType): 85 # def __init__(self,name): 86 # print(‘---------‘) 87 # self.name = name 88 # def f1(self): 89 # print(self.name) 90 # obj = Foo(123) 91 # print(obj) 92 # print(obj.name) 93 ########## 依賴註入###################### 94 class Mapper: 95 __mapper_relation ={} 96 @staticmethod 97 def register(cls,value): 98 Mapper.__mapper_relation[cls]=value 99 @staticmethod 100 def exist(cls): 101 if cls in Mapper.__mapper_relation: 102 return True 103 return False 104 @staticmethod 105 def value(cls): 106 return Mapper.__mapper_relation[cls] 107 class MyType(type): 108 def __call__(cls, *args, **kwargs): 109 obj = cls.__new__(cls,*args,**kwargs) 110 arg_list = list(args) 111 if Mapper.exist(cls): 112 value = Mapper.value(cls) 113 arg_list.append(value) 114 obj.__init__(*arg_list,**kwargs) 115 return obj 116 class Foo(metaclass=MyType): 117 def __init__(self,name): 118 self.name = name 119 def f1(self): 120 print(self.name) 121 122 class Bar(metaclass=MyType): 123 def __init__(self,name): 124 self.name = name 125 def f1(self): 126 print(self.name) 127 Mapper.register(Foo,666) 128 Mapper.register(Bar,999) 129 f = Foo() 130 print(f.name) 131 b= Bar() 132 print(b.name) 133 ##############程序設計原則############# 134 ‘‘‘SOLIP設計原則 135   1、單一責任原則(SRP) 136     一個對象對只應該為一個元素負責 137   2、開放封閉原則(OCP) 138     對擴展開放,修改封閉 139   3、裏氏替換原則(LSP) 140     可以使用任何派生類替換基類 141   4、接口分離原則(ISP) 142     對於接口進行分類避免一個接口的方法過多 143   5、依賴倒置原則(DIP) 144     隔離關系,使用接口或抽象類代指 145   6、依賴註入(DI)和控制反轉原則(ICO) 146     使用鉤子再原來執行流程中註入其他對象 147 ‘‘‘

類補充