Created
January 2, 2015 16:01
-
-
Save def-/c0be89b6b88ebb1159ba to your computer and use it in GitHub Desktop.
Object Inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type # Fine with ref object | |
T = object of RootObj | |
myValue: string | |
S1 = object of T | |
S2 = object of T | |
method speak(x: T) = echo "T Hello ", x.myValue | |
method speak(x: S1) = echo "S1 Meow ", x.myValue | |
method speak(x: S2) = echo "S2 Woof ", x.myValue | |
echo "creating initial objects of types S1, S2, and T" | |
var a = S1(myValue: "Green") | |
a.speak | |
var b = S2(myValue: "Blue") | |
b.speak | |
var u = T(myValue: "Blue") | |
u.speak | |
echo "Making copy of a as u, colors and types should match" | |
u = a | |
u.speak | |
a.speak | |
# This used to output: | |
# | |
# creating initial objects of types S1, S2, and T | |
# S1 Meow Green | |
# S2 Woof Blue | |
# T Hello Blue | |
# Making copy of a as u, colors and types should match | |
# S1 Meow Green | |
# S1 Meow Green | |
# | |
# Now it outputs: | |
# | |
# creating initial objects of types S1, S2, and T | |
# S1 Meow Green | |
# S2 Woof Blue | |
# T Hello Blue | |
# Making copy of a as u, colors and types should match | |
# T Hello Green | |
# S1 Meow Green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment