| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|---|
| 2 |
<html> |
|---|
| 3 |
<head> |
|---|
| 4 |
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
|---|
| 5 |
<link href="pyd.css" rel="stylesheet" type="text/css"> |
|---|
| 6 |
<title>Inheritance</title> |
|---|
| 7 |
</head> |
|---|
| 8 |
|
|---|
| 9 |
<body> |
|---|
| 10 |
%(nav)s |
|---|
| 11 |
<div id="content"> |
|---|
| 12 |
|
|---|
| 13 |
<h1>Inheritance</h1> |
|---|
| 14 |
|
|---|
| 15 |
<p>If you wrap both a class and a child of that class, Pyd is smart enough to make the resulting Python classes have a parent-child relationship. Any methods of the parent class will automatically be available to the child class. If the child class overloads any of those methods, it is important that the user wrap them in the module's init function. For example:</p> |
|---|
| 16 |
|
|---|
| 17 |
<pre class="code"><span class="keyword">import</span> std.stdio; |
|---|
| 18 |
|
|---|
| 19 |
<span class="keyword">class</span> Base { |
|---|
| 20 |
<span class="keyword">void</span> foo() { writefln(<span class="string">"Base.foo"</span>); } |
|---|
| 21 |
<span class="keyword">void</span> bar() { writefln(<span class="string">"Base.bar"</span>); } |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
<span class="keyword">class</span> Derived : Base { |
|---|
| 25 |
<span class="keyword">void</span> foo() { writefln(<span class="string">"Derived.foo"</span>); } |
|---|
| 26 |
}</pre> |
|---|
| 27 |
|
|---|
| 28 |
<p>These would be exposed to Python by putting this code in <code>PydMain</code> after the call to <code>module_init</code>:</p> |
|---|
| 29 |
|
|---|
| 30 |
<pre class="code">wrap_class!( |
|---|
| 31 |
Base, |
|---|
| 32 |
Def!(Base.foo), |
|---|
| 33 |
Def!(Base.bar), |
|---|
| 34 |
); |
|---|
| 35 |
|
|---|
| 36 |
wrap_class!( |
|---|
| 37 |
Derived, |
|---|
| 38 |
Def!(Derived.foo), |
|---|
| 39 |
);</pre> |
|---|
| 40 |
|
|---|
| 41 |
<p>When used in Python, we get the expected behavior:</p> |
|---|
| 42 |
|
|---|
| 43 |
<pre class="code">>>> issubclass(Derived, Base) |
|---|
| 44 |
True |
|---|
| 45 |
>>> b = Base() |
|---|
| 46 |
>>> d = Derived() |
|---|
| 47 |
>>> b.foo() |
|---|
| 48 |
Base.foo |
|---|
| 49 |
>>> b.bar() |
|---|
| 50 |
Base.bar |
|---|
| 51 |
>>> d.foo() |
|---|
| 52 |
Derived.foo |
|---|
| 53 |
>>> d.bar() |
|---|
| 54 |
Base.bar</pre> |
|---|
| 55 |
|
|---|
| 56 |
<p>Polymorphic behavior is also automatically taken care of. Take a function like the following:</p> |
|---|
| 57 |
|
|---|
| 58 |
<pre class="code"><span class="keyword">void</span> polymorphic_call(Base b) { |
|---|
| 59 |
b.foo(); |
|---|
| 60 |
}</pre> |
|---|
| 61 |
|
|---|
| 62 |
<p>And in Python:</p> |
|---|
| 63 |
|
|---|
| 64 |
<pre class="code">>>> <span class="keyword">class</span> PyClass(Base): |
|---|
| 65 |
... <span class="keyword">def</span> foo(self): |
|---|
| 66 |
... <span class="keyword">print</span> <span class="string">"PyClass.foo"</span> |
|---|
| 67 |
... |
|---|
| 68 |
>>> p = PyClass() |
|---|
| 69 |
>>> polymorphic_call(p) |
|---|
| 70 |
PyClass.foo</pre> |
|---|
| 71 |
|
|---|
| 72 |
<p><i>(TODO: Add support for interfaces and abstract classes.)</i></p> |
|---|
| 73 |
|
|---|
| 74 |
</div> |
|---|
| 75 |
|
|---|
| 76 |
</body> |
|---|
| 77 |
</html> |
|---|