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

猜你喜欢

转载自www.cnblogs.com/1521681359qqcom/p/12524381.html