#繼承 #衍生的類(子類)繼承父類的方法和數據成員 #語法: # class 子類:父類 # {......} #子類繼承父類,父類派生子類。父類又叫基類,子類又叫派生類 class Vehicle: def __init__(self,name): self.name =''; print('交通工具被初始化 建立出來'); def run(self): print('交通工具在行駛...'); def show(self): print('車的名字是%s' % self.name); class Truck(Vehicle):#子類(父類);繼承語法 def __init__(self,name,weith):#子類的構造方法應該包含父類和子類的共同屬性 #Vehicle.name = name; # 一、調用父類屬性,在__init__()方法中使用父類.屬性 self.name = name; # 二、調用父類屬性,在__init__()方法中使用self.屬性或父類 #Vehicle.__init__(self.name);#一、調用父類方法 __init__(self,參數) # super(Vehicle, self).__init__(name);#二、一、調用父類方法 super(父類,self).__init__(參數) self.weith=weith; print('卡車被初始化 建立出來'); #方法重寫 def run(self): Vehicle.show(self); print('%s卡車載重%d噸 行駛中...'%(self.name,self.weith)); class Trian(Vehicle): def __init__(self,name,nums): self.nums=nums; def run(self): print('火車在軌道上行駛...'); t=Truck('東風',15); t.run();
class cixunfu: def run(self): print('在磁懸浮軌道上行駛..') class student: def goHome(self,vehicle): vehicle.run() print('學生在回家的途中...') class tui: def run(self): print('2條腿兒在行駛...') # c=Trian('京滬高鐵',30); # c.run(); # ok=Trian('馬航730',30); daizhong=tui(); xinpeng=student(); xinpeng.goHome(daizhong);
寵物看病函數
#寵物看病 class pet: def kanbing(self): print('給寵物看病...'); class Car(pet): def kanbing(self): print('給貓咪看病 吃藥,吃小黃魚..'); class Dog(pet): def kanbing(self): print('給狗狗看病 輸液,吃骨頭..'); class bird(pet): def kanbing(self): print('給小鳥看病 拔罐,吃蟲子..'); class pig(pet): def kanbing(self): print('給小豬看病 鍼灸,吃豬糧..'); class ShouVet: def JiuZhen(self,pet): print('獸醫出診..給寵物看病..'); pet.kanbing(); dingdang=Car(); kala=Dog(); huangli=bird(); peiqi=pig(); syi=ShouVet(); syi.JiuZhen(dingdang); syi.JiuZhen(kala); syi.JiuZhen(huangli); syi.JiuZhen(peiqi);
# isinstance()函數:判斷是不是繼承關係 print('kala is Dog:',isinstance(kala,Dog));
計算器工具
#計算器 class Operation: def __init__(self,numA,numB): self.numA=numA; self.numB=numB; def GetResult(self): print('計算2操做數的運算結果'); class OpeartionAdd(Operation): def __init__(self,numA,numB): super(OpeartionAdd, self).__init__(numA,numB); def GetResult(self): return self.numA+self.numB; class OpeartionSub(Operation): def __init__(self,numA,numB): # super(OpeartionSub, self).__init__(numA,numB); super(OpeartionSub, self).__init__(munA,numB); def GetResult(self): return self.numA-self.numB; class OpeartionMul(Operation): def __init__(self,numA,numB): super(OpeartionMul, self).__init__(numA,numB); def GetResult(self): return self.numA*self.numB; class OpeartionDiv(Operation): def __init__(self,numA,numB): super(OpeartionDiv, self).__init__(numA,numB); def GetResult(self): return self.numA//self.numB; if __name__=='__main__': numA=int(input('請輸入第一個操做數:')); numB=int(input('請輸入第二個操做數:')); fu=input('請輸入運算符:'); q=None;#未賦值的變量 表明一個計算器 if fu=='+': q=OpeartionAdd(numA,numB); elif fu=='-': q = OpeartionSub(numA, numB); elif fu == '*': q = OpeartionMul(numA, numB); elif fu == '/': q = OpeartionDiv(numA, numB); else: print('輸入錯誤'); print('計算結果:',q.GetResult())
#計算器 工廠類 class Operation: def __new__(cls): pass def GetResult(self,numA, numB): print('計算2操做數的運算結果') class OpeartionAdd(Operation): def __new__(cls): return object.__new__(cls); #return Operation.__new__(cls) def GetResult(self,numA,numB): return numA+numB class OperationSub(Operation): def GetResult(self,numA,numB): return numA-numB class OperationMul(Operation): def GetResult(self,numA,numB): return numA*numB class OperationDiv(Operation): def GetResult(self,numA,numB): return numA//numB class OperationFactory(object): #不一樣的參數建立不一樣的對象的 函數 def create_class(self,fu): if fu == '+': q = OpeartionAdd(); elif fu == '-': q = OperationSub(); elif fu == '*': q = OperationMul(); elif fu == '/': q = OperationDiv(); return q; if __name__== '__main__': #工廠對象 factory=OperationFactory(); numA=int(input('請輸入第一個操做數')) numB=int(input('請輸入第二個操做數')) fu = input('請輸入運算符:') # q表明 由 工廠建立出來的一個計算器 q=factory.create_class(fu) print('計算結果:',q.GetResult(numA, numB))
單例模式:該模式主要目的是確保某一個類只有一個實例存在ui
class singleton: #藉助第三方變量 __instance=None; def __new__(cls, *args, **kwargs): if cls.__instance==None: cls.__instance=object.__new__(cls); return cls.__instance; else: return cls.__instance; s=singleton(); print(id(s)); s1=singleton(); print(id(s1));
a,b=10,5; c=a;#藉助第三方變量更改兩個值的位置 a=b; b=c; print(a,b);