-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.py
More file actions
80 lines (60 loc) · 2.19 KB
/
inheritance.py
File metadata and controls
80 lines (60 loc) · 2.19 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Animal:
def __init__(self, birthType="Unknown", appearance="Unknown", blooded="Unknown"):
self.birthType = birthType
self.appearance = appearance
self.blooded = blooded
@property #getter method
def birthType(self):
return self.__birthType
@birthType.setter #setter
def birthType(self, birthType):
self.__birthType = birthType
@property
def appearance(self):
return self.__appearance
@appearance.setter
def appearance(self, appearance):
self.__appearance = appearance
@property
def blooded(self):
return self.__blooded
@blooded.setter
def blooded(self, blooded):
self.__blooded = blooded
def __str__(self): #magic string method
return "A {} is {} it has {} it is {}".format(type(self).__name__, self.birthType, self.appearance, self.blooded)
class Mammal(Animal):
def __init__(self, birthType="born alive", appearance="hair or fur", blooded= "warm blooded", nurseYoung=True):
Animal.__init__(self, birthType, appearance, blooded)
self.__nurseYoung = nurseYoung
@property
def nurseYoung(self):
return self.__nurseYoung
@nurseYoung.setter
def nurseYoung(self, nurseYoung):
self.__nurseYoung = nurseYoung
def __str__(self):
return super().__str__()+" and it is {} they nurse their young".format(self.nurseYoung)
class Reptile(Animal):
def __init__(self, birthType="born in an egg or born alive", appearance="dry scales", blooded= "cold blooded", nurseYoung=True):
Animal.__init__(self, birthType, appearance, blooded)
##function overloading
def sumAll(self, *args):
sum = 0
for i in args:
sum += i
return sum
def getBirthType(theObject): #polymorphism
print("the {} is {}".format(type(theObject).__name__, theObject.birthType))
def main():
animal1 = Animal("born alive")
print(animal1.birthType)
print(animal1)
print()
mammal1 = Mammal()
print(mammal1)
reptile1 = Reptile()
print(reptile1)
getBirthType(mammal1)
getBirthType(reptile1)
main()