モトネタ オブジェクト指向におけるFizzBuzz問題
http://blogs.wankuma.com/episteme/archive/2007/11/08/106927.aspx
とりあえずRubyで…
main.rb
require 'animal.rb'
animal = nil
animal = Dog.new
animal.sound
animal.count = 3
animal.sound
animal = Cat.new
animal.sound
animal.rb
class Animal
attr_accessor :cry, :count
def initialize
self.count = 1
end
def sound
self.count.times { print cry }
puts
end
end
class Dog < Animal
def initialize
super
self.cry = "わん"
end
end
class Cat < Animal
def initialize
super
self.cry = "にゃー"
end
end
カプセル化?そんなの関係ね~!