day6-class继承

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,age
19         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
posted @ 2017-07-15 13:40  marcoxu  阅读(140)  评论(0编辑  收藏  举报