Ruby China
  • Topics
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • Sign Up
  • Sign In
金天
@Edward_jt
VIP
NO. 1344 / 2012-03-09

2 Topics / 3 Replies
0 Followers
0 Following
0 Favorites
No GitHub.
  • Overview
  • Topics
  • Replies
  • Favorites
  • Following
  • Followers
  • [新手的困惑] 复写了 ruby 内建的方法后子类有没有那个方法 at March 12, 2012

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

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

    #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 March 11, 2012

    好吧。。我大概明白了

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