1
0
Fork 0
lrthw/exercises/ex42.rb

103 lines
1.6 KiB
Ruby
Raw Normal View History

2023-09-30 13:20:59 +00:00
# Animal is-a object
class Animal
def initialize(latin_name)
# Animal has-a latin_name
@latin_name = latin_name
end
end
# Dog is-a Animal
class Dog < Animal
def initialize(latin_name, name)
super(latin_name)
# Dog has-a name
@name = name
end
end
# Cat is-a Animal
class Cat < Animal
def initialize(latin_name, name)
super(latin_name)
# Cat has-a name
@name = name
end
attr_reader :name
end
# Person is-a object
class Person
def initialize(name, pet = nil)
# Person has-a name
@name = name
# Person has-a pet of some kind
@pet = pet
end
attr_accessor :pet
end
# Employee is-a Person
class Employee < Person
def initialize(name, salary)
super(name)
@salary = salary
end
end
# class Employee < Person
# def initialize(name, salary)
# # Employee has-a name
# super(name)
# # Employee has-a salary
# @salary = salary
# end
# end
# Fish is-a object
class Fish
end
# Salmon is-a Fish
class Salmon < Fish
end
# Halibut is-a Fish
class Halibut < Fish
end
# rover is-a Dog
rover = Dog.new("Latin", "Rover")
# satan is-a Cat
satan = Cat.new("Latin", "Satan")
# mary is-a Person
mary = Person.new("Mary")
# mary has-a Pet, satan
mary.pet = satan
# frank is-a Employee
frank = Employee.new("Frank", 120_000)
# frank has-a pet, rover
frank.pet = rover
# flipper is-a Fish
flipper = Fish.new
# crouse is-a Salmon
crouse = Salmon.new
# harry is-a Halibut
harry = Halibut.new
# fidget is-a cat
fidget = Cat.new("Latin", "Fidget")
# james is-a Person and has-a pet, Fidget
james = Person.new("James", fidget)
puts james.pet.name # Fidget