mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-04 05:47:53 +02:00
finished first draft :)
This commit is contained in:
@@ -429,14 +429,37 @@ type Panther <: Cat # Panther is also a subtype of Cat
|
|||||||
Panther() = new("green") # Panthers will only have this constructor, and no default constructor.
|
Panther() = new("green") # Panthers will only have this constructor, and no default constructor.
|
||||||
end
|
end
|
||||||
|
|
||||||
####
|
# Multiple Dispatch
|
||||||
#### In progress point
|
|
||||||
####
|
# In Julia, all named functions are generic functions
|
||||||
|
# This means that they are built up from many small methods
|
||||||
|
# For example, let's make a function meow:
|
||||||
|
function meow(cat::Lion)
|
||||||
|
cat.roar # access properties using dot notation
|
||||||
|
end
|
||||||
|
|
||||||
|
function meow(cat::Panther)
|
||||||
|
"grrr"
|
||||||
|
end
|
||||||
|
|
||||||
|
function meow(cat::Tiger)
|
||||||
|
"rawwwr"
|
||||||
|
end
|
||||||
|
|
||||||
|
meow(tigger) #=> "rawwr"
|
||||||
|
meow(Lion("brown","ROAAR")) #=> "ROAAR"
|
||||||
|
meow(Panther()) #=> "grrr"
|
||||||
|
|
||||||
|
function pet_cat(cat::Cat)
|
||||||
|
println("The cat says $(meow(cat))")
|
||||||
|
end
|
||||||
|
|
||||||
|
pet_cat(tigger) #=> ERROR: no method pet_cat(Tiger,)
|
||||||
|
pet_cat(Lion(Panther(),"42")) #=> prints "The cat says 42"
|
||||||
|
|
||||||
#### Multiple Dispatch
|
|
||||||
|
|
||||||
## Further Reading
|
## Further Reading
|
||||||
|
|
||||||
#### Link to Maunual
|
You can get a lot more detail from [The Julia Manual](http://docs.julialang.org/en/latest/manual/)
|
||||||
Still up for more? Try [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user