继承

简介

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
# coding:utf-8

class Parent():
def __init__(self,name,sex):
self.name = name
self.sex = sex
def talk(self):
return f'{self.name}are walking'
def is_sex(self):
if self.sex == 'boy':
return f'{self.name} is a boy'
else:
return f'{self.name} is a gril'

class ChildOne(Parent):
def play_football(self):
return f'{self.name} are playing football'
class ChildTwo(Parent):
def play_pingpong(self):
return f'{self.name} are playing pingpong'

c_one = ChildOne(name='小木',sex='boy')
result = c_one.play_football()
result = c_one.talk()
print(result)

c_two = ChildTwo(name='小云',sex='girl')
result = c_two.play_pingpong()
print(result)

p = Parent(name='小木爸爸',sex='boy')
result = p.talk()
print(result)
result = p.is_sex()
print(result)