SongList容器

InBlock.gifclass Song
InBlock.gif  def initialize(name, artist, duration)
InBlock.gif    @name=name
InBlock.gif    @artist=artist
InBlock.gif    @duration=duration
InBlock.gif  end
InBlock.gif  
InBlock.gif  attr_accessor :name, :duration, :artist 
InBlock.gifend
InBlock.gif
class KaraokeSong < Song
InBlock.gif  def initialize(name, artist, duration, lyrics)
InBlock.gif    super(name, artist, duration)
InBlock.gif    @lyrics=lyrics
InBlock.gif  end
InBlock.gif    
InBlock.gif  attr_writer :lyrics 
InBlock.gif  attr_reader :lyrics
InBlock.gif    
InBlock.gifend
InBlock.gif
class SongList
InBlock.gif  
InBlock.gif  def initialize
InBlock.gif    @songs= Array.new
InBlock.gif  end
InBlock.gif  
InBlock.gif  def append(song)
InBlock.gif    @songs.push(song)
InBlock.gif    self
InBlock.gif  end
InBlock.gif  
InBlock.gif  def delete_first
InBlock.gif    @songs.shift
InBlock.gif  end
InBlock.gif  
InBlock.gif  def delete_last
InBlock.gif    @songs.pop
InBlock.gif  end
InBlock.gif  
InBlock.gif  def [](index)
InBlock.gif    @songs[index]
InBlock.gif  end
InBlock.gifend
InBlock.gif
require 'test/unit'
InBlock.gifclass TestSongList < Test::Unit::TestCase
InBlock.gif  def test_delete
InBlock.gif    list = SongList.new
InBlock.gif    s1 =Song.new('title1','artist1',1)
InBlock.gif    s2 =Song.new('title2','artist2',2)
InBlock.gif    s3 =Song.new('title3','artist3',3)
InBlock.gif    s4 =Song.new('title4','artist4',4)
InBlock.gif    
InBlock.gif    list.append(s1).append(s2).append(s3).append(s4)
InBlock.gif    
InBlock.gif    assert_equal(s1, list[0])
InBlock.gif    assert_equal(s3,list[2])
InBlock.gif    assert_nil(list[9])
InBlock.gif
    assert_equal(s1,list.delete_first)
InBlock.gif    assert_equal(s2,list.delete_first)
InBlock.gif    assert_equal(s4,list.delete_last)
InBlock.gif    assert_equal(s3,list.delete_last)
InBlock.gif  end
InBlock.gifend
InBlock.gif




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/64010,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值