Created
January 5, 2010 11:01
-
-
Save sergiolopes/269317 to your computer and use it in GitHub Desktop.
exemplo de polimorfismo e duck typing
This file contains 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
class Pato | |
def quack | |
puts 'quack' | |
end | |
end | |
class PatoEstranho | |
def quack | |
puts 'quaaaack' | |
end | |
end | |
# uso normal | |
p = Pato.new | |
p.quack | |
p2 = PatoEstranho.new | |
p2.quack | |
# usando o polimorfismo | |
def fazQuack(pato) | |
pato.quack | |
end | |
fazQuack(Pato.new) | |
fazQuack(PatoEstranho.new) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment