码迷,mamicode.com
首页 > 其他好文 > 详细

Ruby中类的静态方法与继承

时间:2020-03-19 15:07:15      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:def   tle   put   str   ali   his   info   静态方法   sel   

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中类的静态方法与继承

标签:def   tle   put   str   ali   his   info   静态方法   sel   

原文地址:https://www.cnblogs.com/1521681359qqcom/p/12524381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!