betway必威-betway必威官方网站
做最好的网站

Setter with =

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

本文由betway必威发布于编程开发,转载请注明出处:Setter with =

TAG标签: betway必威
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。