[学习]python类,继承

[学习]python类,继承

转载https://www.yuanrenxue.com/python/python-class-inheritance.html

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
class Person:
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height

def look(self):
print(self.name, 'is looking')

def walk(self):
print(self.name, 'is walking')

class Teacher(Person):
def __init__(self, name, age, height):
super().__init__(name, age, height)

def teach(self):
print(self.name, 'is teaching')

class Student(Person):
def __init__(self, name, age, height):
super().__init__(name, age, height)

def learn(self):
print(self.name, 'is learning')


if __name__ == '__main__':
teacher = Teacher('Tom', 31, 178)
s1 = Student('Jim', 12, 160)
s2 = Student('Kim', 13, 162)

teacher.look()
teacher.walk()
teacher.teach()
print('==='*5)

s1.look()
s1.walk()
s1.learn()
print('==='*5)

s2.look()
s2.walk()
s2.learn()

欢迎访问我的博客  地址      
https://blog.afacode.top