Ruby China
  • 社区
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • 注册
  • 登录
金天
@Edward_jt
高级会员
第 1344 位会员 / 2012-03-09

2 篇帖子 / 3 条回帖
0 关注者
0 正在关注
0 收藏
未设置 GitHub 信息。
  • 概况
  • 话题
  • 回帖
  • 收藏
  • 正在关注
  • 关注者
  • [新手的困惑] 复写了 ruby 内建的方法后子类有没有那个方法 at 2012年03月12日

    #5 楼 @xguox 这样子啊~现在明白了。谢谢~

  • [新手的困惑] 复写了 ruby 内建的方法后子类有没有那个方法 at 2012年03月12日

    #3 楼 @ery 谢谢。 话说能不能举个例子(还是不大明白)

    class Song
      def initialize(name,artist,duration)
      end
    end
    song=Song.new("Bicycle",:"Fleck",260)
    puts song.to_s 
    class Song
      def to_s
        "Song:#{name}--#{artist}(#{duration})"
      end
    end
    puts song.to_s 
    class KaraokeSong<Song
      def initialize(name,artist,duration,lyrics)
        super(name,artist,duration)
      end
    end
    newsong=KaraokeSong.new("My Way","Sinatra",255,"And now,the...")
    puts newsong.to_s 
    
    
    

    我把代码改成这个样子后 然后报错 ‘rb:9:in to_s': undefined local variable or methodname' for #Song:0x9357bdc (NameError)”’ 这个错误是什么意思。。。是因为没把局部变量变成实例变量吗。?

  • [新手的困惑] 复写了 ruby 内建的方法后子类有没有那个方法 at 2012年03月11日

    好吧。。我大概明白了

关于 / RubyConf / Ruby 镜像 / RubyGems 镜像 / 活跃会员 / 组织 / API / 贡献者
由众多爱好者共同维护的 Ruby 中文社区,本站使用 Homeland 构建,并采用 Docker 部署。
服务器由 赞助 CDN 由 赞助
iOS 客户端 / Android 客户端 简体中文 / English