`

ruby---类方法定义

    博客分类:
  • Ruby
 
阅读更多
# -*- coding: utf-8 -*-
# ruby定义类方法的三中形式

=begin
方法一: def 类名.方法名~end"
=end
class HelloWorld
  def HelloWorld.hello(name)
    print name, " said Hello."
  end
end

jack = HelloWorld.hello("jack")

=begin
方法二: class << 类名~def 方法名~end end"
=end
class HelloWorld2
  
end
class << HelloWorld2
  def hello(name)
    print name, " said Hello2."
  end
end

tony = HelloWorld2.hello("tony")

=begin
方法三: class 类名~def self.方法名~end end
=end
class HelloWorld3
  def self.hello(name)
    print name, " said Hello3."
  end
end
cherry = HelloWorld3.hello("cherry")
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics