Created
August 29, 2023 09:16
-
-
Save Dickyrdiar/c71edc109691db225a1b7b0761b40fb8 to your computer and use it in GitHub Desktop.
pholymorphism example
This file contains hidden or 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
class Rectangle < Shape | |
attr_accessor :width, :height | |
def initialize(width, height) | |
@width = width | |
@height = height | |
end | |
def area | |
@width * @height | |
end | |
end | |
class Circle < Shape | |
attr_accessor :radius | |
def initialize(radius) | |
@radius = radius | |
end | |
def area | |
Math::PI * @radius**2 | |
end | |
end | |
shapes = [Rectangle.new(5, 10), Circle.new(3)] | |
shapes.each do |shape| | |
puts "Area: #{shape.area}" | |
e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment