Skip to content

Commit 01b5dc8

Browse files
committed
Added a slide about super() in class 4 and some details about overriding
methods
1 parent fa5df40 commit 01b5dc8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

class4.html

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ <h3>Inheritance</h3>
203203

204204
<section>
205205
<h3>Inheritance Continued</h3>
206-
<p></p>
206+
<p>The move_up method is <strong>overridden</strong> in the child class below:</p>
207207
<pre><code contenteditable class="fragment python">
208208
class BoundedMobile(Mobile):
209209
"""
@@ -219,6 +219,24 @@ <h3>Inheritance Continued</h3>
219219
<p class="fragment">See <a href="http://calebsmith.github.io/gdi-intro-python/examples/mobile.py">mobile.py</a> for a more complete example.</p>
220220
</section>
221221

222+
<section>
223+
<h3>What's Super about Super</h3>
224+
<p><strong>super</strong> is often helpful when writing methods that override the method of the parent class</p>
225+
<pre><code contenteditable class="fragment python">
226+
class BoundedMobile(Mobile):
227+
"""
228+
An object with an x, y position, and methods for moving
229+
The x, y position must be within bounds
230+
"""
231+
232+
def move_up():
233+
super(BoundedMobile, self).move_up()
234+
if self.y < 0:
235+
self.y = 0
236+
</code></pre>
237+
<p class="fragment">The call to super() takes the name of the child class, followed by self. This is followed by the method call and any arguments to pass to it</p>
238+
</section>
239+
222240
<section>
223241
<h3>Composition</h3>
224242
<p>Classes can also use the technique of <strong>composition</strong></p>

0 commit comments

Comments
 (0)