-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses_inheritance.py
90 lines (68 loc) · 1.92 KB
/
classes_inheritance.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
# -*- coding: utf-8 -*-
"""
Simple study related do classes inheritance - creating subclasses
"""
# General Class
class Warrior(object):
# delta position - class variable
delta_position = 10
# number of warriors
num_of_warriors = 0
def __init__(self,posx,posy):
self.posx = posx
self.posy = posy
Warrior.num_of_warriors+=1
def move(self, posx,posy):
self.posx = posx+ self.delta_position
self.posy = posy +self.delta_position
def position(self):
return '{} , {}' .format(self.posx,self.posy)
# First Example
class Archer(Warrior):
pass
# Second Example
class Horseman(Warrior):
def __init__(self,posx,posy, attack):
# use the constructor of class Warrior use of super
super().__init__(posx, posy)
self.attack = attack
if __name__ == "__main__":
# First Example
archer = Archer(100,10)
print(archer.posx)
# printing the Method resolution order
# print(help(archer))
"""
class Archer(Warrior)
| Method resolution order:
| Archer
| Warrior
| builtins.object
|
| Methods inherited from Warrior:
|
| __init__(self, posx, posy)
|
| move(self, posx, posy)
|
| position(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Warrior:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from Warrior:
|
| delta_position = 10
|
| num_of_warriors = 1
"""
# Second Example
horseman = Horseman(10,20,56)
print(horseman.attack)