-
Notifications
You must be signed in to change notification settings - Fork 27
/
4-inheritance.rb
56 lines (48 loc) · 1.29 KB
/
4-inheritance.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# ================================
# Inheritance
#
# classes can inherit values and
# behavior from another class. this
# is one of the primary strengths
# of object oriented programming.
# ================================
# ================================
# simple inheritance
# ================================
class Animal # Animals can move
def move
puts "moving..."
end
end
class Bird < Animal # a Bird is an animal
def fly
puts "flying..."
end
end
roy = Bird.new # Birds can move and fly
roy.move
roy.fly
# ================================
# more inheritance
# ================================
class Runner
def speed
4.0
end
end
# a Sprinter (by our definition) runs 2X faster than a general Runner
class Sprinter < Runner
def speed
super() * 2.0 # super() returns the base (Runner's) speed
end # our Sprinter runs twice that speed
end
# a Jogger (by our definition) runs at 3/4 of the speed of a Runner
class Jogger < Runner
def speed
super() * 0.75 # super() returns the base (Runner's) speed
end # our Jogger is 0.75 times that speed
end
steve = Jogger.new
puts "steve, the jogger runs at #{steve.speed} mph"
mike = Sprinter.new
puts "mike, the sprinter runs at #{mike.speed} mph"