Ruby中类的静态方法与继承

Posted 1521681359qqcom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby中类的静态方法与继承相关的知识,希望对你有一定的参考价值。

class Game
    def initialize(id,title,price)
        @id=id
        @title=title
        @price=price
    end

    def showGame
        puts @id+" "+@title+","+@price._to_s
    end

    def self.toStr
        puts "I love this game."
    end 
end

zelda=Game.new("zelda","sister",unlimit)
zelda.showGame
Game.toStr
Game::toStr

执行结果

技术图片

 

 

class Game
    def initialize(id,title,price)
        @id=id
        @title=title
        @price=price
    end
    def showGame
        puts @id+@title+@price.to_s
    end

    def self.toStr
        puts "I Love this game."
    end 
end

class SteamGame<Game
    def SteamInf
         puts "i am a good man"
    end
end

SteamGame.toStr

mygame=SteamGame.new("nobunage-taishi","sister",3000)
mygame.showGame
mygame.SteamInf

 

以上是关于Ruby中类的静态方法与继承的主要内容,如果未能解决你的问题,请参考以下文章