Setter with =

2019-06-19 20:03栏目:技术创新

class Song
  include Comparable
  @@plays = 0
  attr_reader :name, :artist, :duration
  attr_writer :duration
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
    @plays    = 0
  end
  def to_s
    'Song: #@name--#@artist (#@duration)'
  end
  def duration=(new_duration)
    @duration = new_duration
  end
  def inspect
    self.to_s
  end
end
song = Song.new('Bicylops', 'Fleck', 260)
song.duration
song.duration = 257   # set attribute with updated value
song.duration

版权声明:本文由新浦京娱乐站_新浦京娱乐场官网_新浦京娱乐发布于技术创新,转载请注明出处:Setter with =