继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

ruby静态方法的4种写法与单例方法的2种写法

月关宝盒
关注TA
已关注
手记 232
粉丝 104
获赞 673
#静态方法的4种写法
class Test
  def Test.StaticMethod1
    puts "Test.StaticMethod1"
  end
   
  def self.StaticMethod2
    puts "Test.StaticMethod2"
  end
   
  class << Test
    def StaticMethod3
      puts "Test.StaticMethod3"
    end
  end
   
  class << self
    def StaticMethod4
      puts "Test.StaticMethod4"
    end
  end
end
   
Test.StaticMethod1
Test.StaticMethod2
Test.StaticMethod3
Test.StaticMethod4
#单例方法的2种写法
 
class Test
  def method1
    puts "method1"
  end
end
 
t1 = Test.new
 
def t1.singleMethod1
  puts "t1.singleMethod1"
end
 
class << t1
  def singleMethod2
    puts "t1.singleMethod2"
  end
end
 
t2 = Test.new
 
t1.method1
t2.method1
t1.singleMethod1
t1.singleMethod2
#t2.singleMethod1 #将报错
#t2.singleMethod2 #将报错


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP