Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Polymorphism/Ruby/polymorphism-1.rb
Normal file
23
Task/Polymorphism/Ruby/polymorphism-1.rb
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class Point
|
||||
attr_accessor :x,:y
|
||||
def initialize(x=0, y=0)
|
||||
self.x = x
|
||||
self.y = y
|
||||
end
|
||||
def to_s
|
||||
"Point at #{x},#{y}"
|
||||
end
|
||||
end
|
||||
|
||||
# When defining Circle class as the sub-class of the Point class:
|
||||
class Circle < Point
|
||||
attr_accessor :r
|
||||
def initialize(x=0, y=0, r=0)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.r = r
|
||||
end
|
||||
def to_s
|
||||
"Circle at #{x},#{y} with radius #{r}"
|
||||
end
|
||||
end
|
||||
15
Task/Polymorphism/Ruby/polymorphism-2.rb
Normal file
15
Task/Polymorphism/Ruby/polymorphism-2.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# create a point
|
||||
puts Point.new # => Point at 0,0
|
||||
p = Point.new(1, 2)
|
||||
puts p # => Point at 1,2
|
||||
puts p.x # => 1
|
||||
p.y += 1
|
||||
puts p # => Point at 1,3
|
||||
|
||||
# create a circle
|
||||
c = Circle.new(4,5,6)
|
||||
# copy it
|
||||
d = c.dup
|
||||
d.r = 7.5
|
||||
puts c # => Circle at 4,5 with radius 6
|
||||
puts d # => Circle at 4,5 with radius 7.5
|
||||
Loading…
Add table
Add a link
Reference in a new issue