Ruby課題 11月25日

user-pic
0
1.
#!/usr/bin/ruby
class Counter
  def initialize
    @count=0
  end

  def count
    @count
  end

  def setCount(num=0)
    @count=num
  end

  def getCount
    puts @count
  end

  def increase
    @count+=1
  end

  def decrease
    @count-=1
    if @count<=0
      @count=0
    end
  end
end

oita=Counter.new
oita.setCount(10)
oita.getCount
oita.increase
oita.getCount
oita.increase
oita.getCount

実行結果
10
11
12

2.
#!/usr/bin/ruby

require "07-1"
class NewCounter < Counter
  def increase(num=1)
    @count+=num
  end

  def decrease(num=1)
    @count-=num
    if @count<=0
      @count=0
    end
  end

  def time(num)
    @count*=num
  end

  def div(num)
    @count/=num
  end
end

oita=NewCounter.new
oita.setCount(100)
oita.getCount
oita.time(3)
oita.getCount
oita.div(2)
oita.getCount

実行結果
10
11
12
100
300

トラックバック(0)

トラックバックURL: http://www.olnr.org/~hhamabe/blog/mt-tb.cgi/8

コメントする

このブログ記事について

このページは、大吾郎が2008年12月 2日 15:12に書いたブログ記事です。

ひとつ前のブログ記事は「Ruby課題 11月18日」です。

次のブログ記事は「12月9日 Ruby課題」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。