-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple_Inheritance_example.py
142 lines (118 loc) · 3.98 KB
/
multiple_Inheritance_example.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Use 'super()' with simple inheritance and
# if you want to use multiple inheritance call from the son class with a
# instance of constructors from Father classes
class Father:
"""
First base class
"""
def __init__(self, cara, ojosazules, nariz, estatura):
self.cara = cara
self.ojosazules = ojosazules
self.nariz = nariz
self.estatura = estatura
@staticmethod
def my_description(): # this a static method, because don't change output of the method
print("I am the father")
def iam(self): # this is a instance method because have 'self'
print("cara: {} \nojo: {} \nNariz: {} \nEstatura: {}".format(
self.cara,
self.ojosazules,
self.nariz,
self.estatura
))
class Mother:
"""
second base class
"""
def __init__(self, caracter, color_cabello):
self.caracter = caracter
self.color_cabello = color_cabello
@staticmethod
def my_description(): # this a static method, because don't change output of the method
print("I am the Mother")
def iam(self): # this is a instance method because have 'self'
print("caracter : {} \ncabello: {}".format(
self.caracter,
self.color_cabello
))
class Son(Father, Mother): # preference to Father
"""
Derived class from Father and Mother
Son -> Father and Son -> mother (This behavior is multiple inheritance)
"""
def __init__(self, cara, ojosazules, nariz, estatura, caracter, color_cabello, arte):
Father.__init__(self, cara, ojosazules, nariz, estatura) # attribute from Father
Mother.__init__(self, caracter, color_cabello)
self.arte = arte
@staticmethod
def my_description():
print(" I am the son")
def iam(self): # this is a instance method because have 'self'
print("cara: {} \nojo: {} \nNariz: {} \nEstatura: {} \n"
"caracter : {} \ncabello: {} \narte: {}".format(
self.cara,
self.ojosazules,
self.nariz,
self.estatura,
self.caracter,
self.color_cabello,
self.arte
))
class Daughter(Mother, Father): # preference to Mother if create a 'super'
"""
Derived class from Father and Mother
Daughter -> Father and Daughter -> Mother (This behavior is multiple inheritance)
"""
def __init__(self, cara, ojosazules, nariz, estatura, caracter, color_cabello, science):
Mother.__init__(self, caracter, color_cabello) # this the attribute from Mother
Father.__init__(self, cara, ojosazules, nariz, estatura)
self.science = science
@staticmethod
def my_description():
print("I am the Daughter")
def iam(self): # this is a instance method because have 'self'
print("cara: {} \nojo: {} \nNariz: {} \nEstatura: {} \n"
"caracter : {} \ncabello: {} \nscience: {}".format(
self.cara,
self.ojosazules,
self.nariz,
self.estatura,
self.caracter,
self.color_cabello,
self.science
))
if __name__ == '__main__':
# out of class
father = Father(
"cara de padre",
"ojos de padre",
"nariz de padre",
"estatura de padre")
father.my_description()
father.iam()
print("-"*40)
mother = Mother("caracter de madre", "brown")
mother.my_description()
mother.iam()
print("-"*40)
son = Son(
"cara de hijo",
"ojo de hijo",
"nariz de hijo",
"estatura de hijo",
"caracter de hijo",
"cabello de hijo",
"Me gusta la Pintura")
son.my_description()
son.iam()
print("-"*40)
daughter = Daughter(
"cara de hija",
"ojo de hija",
"nariz de hija",
"estatura de hija",
"caracter de hija",
"cabello de hija",
"Me gusta la ciencia")
daughter.my_description()
daughter.iam()