Ruby unterstützt nur einfache Vererbung.
class Gebilde def initialize(x, y) @x, @y = x, y end attr_accessor :x, :y end class Rechteck < Gebilde def initialize(x, y, l, b) super(x, y) @l, @b = l, b end def flaeche @l * @b end end class Kreis < Gebilde def initialize(x, y, r) super(x, y) @r = r end def flaeche 2 * Math::PI * @r**2 end end class Komposition < Gebilde def initialize(gebilde) @gebilde = gebilde end def flaeche a = 0 @gebilde.each { |g| a += g.flaeche } a end end rechteck = Rechteck.new(1, 1, 5, 3) kreis = Kreis.new(1, 9, 4) gruppe = Komposition.new([kreis, rechteck]) puts "Gesamtfläche: #{gruppe.flaeche}"
Gesamtfläche: 115.5309649148734