Inheritance/Single: Difference between revisions
Content deleted Content added
Omit Axe |
→{{header|TXR}}: There is OOP now with structs. |
||
Line 1,679: | Line 1,679: | ||
For exception symbols only. |
For exception symbols only. |
||
====Inheritance among symbolic exception tags==== |
|||
<lang txr>@(defex cat animal) |
<lang txr>@(defex cat animal) |
||
Line 1,696: | Line 1,698: | ||
<pre>$ txr dog-cat.txr |
<pre>$ txr dog-cat.txr |
||
arg="x"</pre> |
arg="x"</pre> |
||
====OOP Inheritance in TXR Lisp==== |
|||
<lang txr>(defstruct animal nil |
|||
name |
|||
(get-name |
|||
(lambda (me) |
|||
(if me.name me.name (error `get-name: animal @me has no name`)))) |
|||
(speak |
|||
(lambda (me stream) |
|||
(error "abstract animal cannot speak")))) |
|||
(defstruct dog animal |
|||
(speak |
|||
(lambda (me : (stream *stdout*)) |
|||
(put-line `@{me.(get-name)}: bark!` stream)))) |
|||
(defstruct cat animal |
|||
(speak (lambda (me : (stream *stdout*)) |
|||
(put-line `@{me.(get-name)}: meow!` stream)))) |
|||
(defstruct lab dog) |
|||
(defstruct collie dog) |
|||
(let ((pet1 (new collie name "Lassie")) |
|||
(pet2 (new cat name "Max"))) |
|||
pet1.(speak) |
|||
pet2.(speak))</lang> |
|||
{{out}} |
|||
<pre>Lassie: bark! |
|||
Max: meow!</pre> |
|||
=={{header|Vorpal}}== |
=={{header|Vorpal}}== |