day6-class繼承
阿新 • • 發佈:2017-07-15
bject mar all def tee lee tro att 如果
Part1---單個父類class繼承
1 class people: 2 def __init__(self,name,age): 3 self.name=name 4 self.age=age 5 6 def eat(self): 7 print(‘%s is eating...‘%self.name) 8 def talking(self): 9 print(‘%s is talking...‘%self.name) 10 def sleeping(self): 11 print(‘%s is sleeping under class people...‘%self.name) 12 13 14 class man(people): #一個class直接調用了另一個已有的class,就像是函數嵌套,只不過這裏是內嵌class(繼承) 15 #pass part1,啥都不寫,後面直接m1.eat() 16 17 def __init__(self,name,age,money): #part3 18 #people.__init__(self,name,age) #方法1經典寫法,調用父位people中的name,age19 super(man,self).__init__(name,age) #方法2新式寫法,用super,內建的方法,格式:super(自己class名,self).__init__(父位中的形參) 20 #好處1:當父位class的名字更改後不會影響到這裏,因為這裏不需要寫父位名 21 #好處2:一個class可以繼承多個父位,如果用上面的方法則要寫多個父位.__init__() 22 self.muchmoney=money #這裏只用寫自己多出來的形參 23 24 def USD(self): #part3 25 print(‘%s has much %s money!!!‘ % (self.name, self.muchmoney)) 26 27 def shout(self): #part2 當然還可以在自己的class下面定義自己的方法 28 print(‘%s is shouting...‘%self.name) 29 def sleeping(self): #part2 c1, 重寫,定義跟繼承class中同樣名字的函數方法 自己的會精細,override 30 people.sleeping(self) #part2 c2, 重構, 如果還想保留繼承class中的東西,但又添加自己的,這裏引用繼承class中的函數方法 31 print(‘%s is sleeping under class man...‘%self.name) 32 33 34 class women(people): #part2 35 def birth(self): 36 print(‘%s is born baby...‘%self.name) 37 38 39 #m1=man(‘alex‘,22) 40 # m1.eat() #part1, #alex is eating... 41 # m1.shout() #part1, #alex is shouting... 42 # #m1.sleeping() #part2 c1, alex is sleeping under class man... 43 # m1.sleeping() #part2 c2, 44 # # alex is sleeping under class people... 45 # # alex is sleeping under class man... 46 # 47 # w1=women(‘alley‘,20) 48 # w1.birth() #alley is born baby... 49 # #w1.shout() #man和women同屬於父class people,子class之間無法互相調用函數方法, 但可以連起來(參見下繼承-2.py中的例子) 50 # #AttributeError: ‘women‘ object has no attribute ‘shout‘ 51 52 53 m1=man(‘alex‘,22,‘10w‘) 54 m1.USD() #alex has much 10w money!!!
Part2---多個父類class繼承
1 #class多父位繼承,以下例子中class man/women繼承了2個父位:people和couple 2 3 class people: 4 def __init__(self,name): 5 self.abc=name 6 self.fridends=[] 7 8 class couple(object): #理解成(object)=__init__(self) 9 def marry(self,obj): 10 print(‘%s and %s are couple‘ %(self.abc,obj.abc)) 11 #這裏調用了class people中的self.abc,又加了個obj.abc. 註意這裏的abc必須跟class people中的abc名字一樣 12 #那abc究竟指啥呢? 由於m1.marry(w1)中的w1會賦值給第二個%s,但前提是couple這個class中沒有定義 13 #自己的構造函數,也就是沒def __init__(self), 如果ojb.xxx=class people中的self.xxx=name=‘w1=women(‘alley‘)‘的話, 14 #那就相當於將alley賦值給abc了 15 16 #下面的例子更改成obj.xxxxx,但不更改class people中的self.abc,結果會是在women object中找不到xxxxx這個靜態屬性 17 #print(‘%s and %s are couple‘ %(self.abc,obj.xxxxx)) 18 #AttributeError: ‘women‘ object has no attribute ‘xxxxx‘ 19 20 #self.fridends.append(obj) #print-1 21 self.fridends.append(obj.abc) #print-2,打印obj中的abc,結果是alley 22 23 #class man(people,couple): 24 class man(couple,people): #位置不會有影響,couple裏面找不到 25 pass 26 27 #class women(people,couple): 28 class women(couple, people): 29 pass 30 31 32 m1=man(‘alex‘) 33 w1=women(‘alley‘) 34 m1.marry(w1) 35 36 #m1.marry(‘afafaf‘) 37 # File "C:/x-working/pycharm/project-14/Day6/繼承-2.py", line 10, in marry 38 # print(‘%s and %s are couple‘ %(self.abc,obj.abc)) 39 # AttributeError: ‘str‘ object has no attribute ‘abc‘ 40 41 #print(m1.fridends[0]) #print-1 42 #<__main__.women object at 0x0000000002939D68> 43 44 print(m1.fridends[0]) #print-2 45 #alley
day6-class繼承