-
Notifications
You must be signed in to change notification settings - Fork 0
/
魔法方法-属性-迭代器
59 lines (55 loc) · 1.03 KB
/
魔法方法-属性-迭代器
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
52
53
54
55
56
57
58
#魔法方法、属性、迭代器的学习
#构造方法
#!/usr/bin/python
class FooBar():
def __init__(self, somevar=2):
self.somevar = somevar
f = FooBar()
print(f.somevar)
f2 = FooBar("This is f2")
print(f2.somevar)
#================
#重写一般方法
#!/usr/bin/python
class A:
def hello(self):
print("Hello, I'm A")
class B(A):
pass
a = A()
b = B()
a.hello()
b.hello()
######
#!/usr/bin/python
class A:
def hello(self):
print("Hello, I'm A")
class B(A):
def hello(self):
print("Hello, I'm B")
a = A()
b = B()
a.hello()
b.hello()
#===============================
#
#!/usr/bin/python
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print("Aaaah")
self.hungry = False
else:
print("No. thanks!")
class SongBird(Bird):
def __init__(self):
super(SongBird, self).__init__()
self.sound = "Squawk!"
def sing(self):
print(self.sound)
sb = SongBird()
sb.sing()
sb.eat()