Created
October 14, 2012 00:09
-
-
Save emberlzhang/3886693 to your computer and use it in GitHub Desktop.
Solution to Simple Matter of Inheritance
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 Animal | |
attr_accessor :awake_at | |
def initialize | |
@awake_at = "day" | |
end | |
def winged? | |
false | |
end | |
# def get_tooth_replacement_count | |
# if has_teeth? | |
# @tooth_replacement > 1 | |
# end | |
# end | |
end | |
class Mammal < Animal | |
def initialize | |
@skeletal_structure = "vertebrae" | |
end | |
def has_fur? | |
true | |
end | |
def warm_blooded? | |
true | |
end | |
def has_teeth? | |
true | |
end | |
def tooth_replacement? | |
@tooth_replacement < 2 | |
end | |
end | |
class Bat < Mammal | |
def initialize | |
@awake_at = 'night' | |
end | |
def winged? | |
true | |
end | |
def has_teeth? | |
true | |
end | |
end | |
class Primate < Mammal | |
attr_accessor :num_legs | |
def initialize | |
@num_legs = 2 | |
end | |
end | |
class Chimpanzee < Primate | |
def locomotion | |
"Knuckle walking" | |
end | |
end | |
class Amphibian < Animal | |
def has_teeth? | |
false | |
end | |
def warm_blooded? | |
false | |
end | |
end | |
class Frog < Amphibian | |
def locomotion | |
"Jumps" | |
end | |
def food | |
"Flies" | |
end | |
end | |
drew = Chimpanzee.new | |
puts drew.num_legs | |
puts drew.locomotion | |
janet = Amphibian.new | |
puts janet.has_teeth? | |
puts janet.warm_blooded? | |
taz = Frog.new | |
puts taz.has_teeth? | |
puts taz.locomotion | |
puts taz.food | |
batman = Bat.new | |
puts batman.awake_at | |
puts batman.has_teeth? | |
puts batman.winged? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment