-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuper.py
More file actions
51 lines (43 loc) · 978 Bytes
/
super.py
File metadata and controls
51 lines (43 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
' a super function test module '
__author__ = 'lmm'
__metaclass__ = type
class Bird:
name = 'lmm'
def __init__(self):
self.hungry = True
# name = '234'
def eat(self):
if self.hungry:
print 'Aaaah..'
self.hungry = False
else:
print 'no thanks'
def out(self):
print 'bird out'
# 旧版python
# class SongBird(Bird):
# def __init__(self):
# Bird.__init__(self)
# self.sound = 'Squawk!'
# def sing(self):
# print self.sound
# def out(self):
# super(SongBird,self).out()
# 新版python使用super调用父类的构造方法
class SongBird(Bird):
def __init__(self):
super(SongBird,self).__init__()
self.sound = 'Squawk!'
def sing(self):
print self.sound
# b = Bird()
# print b.name
sb = SongBird()
sb.sing()
print(sb.hungry)
sb.eat()
sb.eat()
print(sb.hungry)
sb.out()
print sb.name