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中类的静态方法与继承的主要内容,如果未能解决你的问题,请参考以下文章

JDBC连接字符串中类和方法的Ruby继承

Ruby元编程

es6中类中的静态属性实例属性静态方法实例方法的个人理解

java中类的封装与继承

Python中类的内置方法与继承关系实例

Python中类的继承及类的属性和方法总结