-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exercise 42: Is-A, Has-A, Objects, and Classes
- Loading branch information
1 parent
d7faff2
commit 140193b
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Animal is-a object look at the extra credit | ||
class Animal | ||
end | ||
|
||
# ?? | ||
class Dog < Animal | ||
|
||
def initialize(name) | ||
# ?? | ||
@name = name | ||
end | ||
end | ||
|
||
# ?? | ||
class Cat < Animal | ||
|
||
def initialize(name) | ||
# ?? | ||
@name = name | ||
end | ||
end | ||
|
||
# ?? | ||
class Person | ||
|
||
def initialize(name) | ||
# ?? | ||
@name = name | ||
|
||
# Person has-a pet of some kind | ||
@pet = nil | ||
end | ||
|
||
attr_accessor :pet | ||
end | ||
|
||
# ?? | ||
class Employee < Person | ||
|
||
def initialize(name, salary) | ||
# ?? hmm what is this strange magic? | ||
super(name) | ||
# ?? | ||
@salary = salary | ||
end | ||
|
||
end | ||
|
||
# ?? | ||
class Fish | ||
end | ||
|
||
# ?? | ||
class Salmon < Fish | ||
end | ||
|
||
# ?? | ||
class Halibut < Fish | ||
end | ||
|
||
|
||
# rover is-a Dog | ||
rover = Dog.new("Rover") | ||
|
||
# ?? | ||
satan = Cat.new("Satan") | ||
|
||
# ?? | ||
mary = Person.new("Mary") | ||
|
||
# ?? | ||
mary.pet = satan | ||
|
||
# ?? | ||
frank = Employee.new("Frank", 120000) | ||
|
||
# ?? | ||
frank.pet = rover | ||
|
||
# ?? | ||
flipper = Fish.new() | ||
|
||
# ?? | ||
crouse = Salmon.new() | ||
|
||
# ?? | ||
harry = Halibut.new() |