-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex44e.rb
More file actions
57 lines (46 loc) · 1.43 KB
/
ex44e.rb
File metadata and controls
57 lines (46 loc) · 1.43 KB
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
57
# This uses class 'Other' instead of class 'Parent' as in ex44d.rb
# This is because the class 'Child' does not have an
# is-a relationship with the base class, it has a has-a relationship
# Here we have a Child has-a Other relationship instead.
class Other
def override()
puts "OTHER override"
end
def implicit()
puts "OTHER implicit"
end
def altered()
puts "OTHER altered"
end
end
# All of the above is fairly obvious, we define a class 'Other' which has all of the
# functions we may want to use in another class.
class Child
def initialize()
@other = Other.new()
end
# This confused me for a minute.
# We are setting a function that initializes the use of the Other class.
# We use @other to allow us to use it in other functions.
# We set @other to a new instance of the 'Other' class, then end.
def implicit()
@other.implicit()
end
# Here we are using the @other variable we set above, and calling the
# implicit function from it. This will just print out whatever is inside Other
def override()
puts "CHILD override"
end
# This one uses the same funtion name as Other does, so will override it.
def altered()
puts "CHILD, BEFORE altered"
@other.altered()
puts "CHILD, AFTER altered"
end
end
son = Child.new()
# son variable is a new instance of the Child class
son.implicit()
son.override()
son.altered()
# Finally we throw out all of the contents of son's different functions