细说Ruby里的public、protected和private

首先来看这段代码:
class Test    
  def method_public
    puts  "In method_public"    
  end  
 
  def method_protected
    puts "In method_protected"     
  end  
 
  def method_private
    puts "In method_private"   
  end
 
  protected :method_protected
  private   :method_private
end

test=Test.new

分别尝试:

        test.method_public
        输出:In method_public

        test.method_protected
        输出:test.rb:20: protected method `method_protected' called for #<Test:0x3756068> (NoMethodError)

        test.method_private

        输出:test.rb:21: private method `method_private' called for #<Test:0x346070> (NoMethodError)

可知:public能被实例对象调用,protected和private不能被实例对象直接调用。

改写代码:

class Test    
  def method_public
    puts  "In method_public" 
  end
  
  def method_protected
    puts "In method_protected"     
  end  
  
  def method_private
    puts "In method_private"   
  end
 
  protected :method_protected
  private     :method_private
  
  def access_method
    puts method_public
    puts method_protected    
    puts method_private
  end
  
end

test = Test.new
test.access_method
        输出:

        In method_public
        nil
        In method_protected
        nil
        In method_private
        nil

可知:三种方式都能被定义它们的类访问。

改写代码:

class Test    
  def method_public
    puts  "In method_public" 
  end
  
  def method_protected
    puts "In method_protected"     
  end  
  
  def method_private
    puts "In method_private"   
  end
 
  protected :method_protected
  private   :method_private
end

class SonTest < Test
  def access_method
    puts method_public
    puts method_protected    
    puts method_private
  end
end

test = SonTest.new
test.access_method
        输出:

       In method_public
       nil
       In method_protected
       nil
       In method_private
       nil
可知:三种方法都能被定义它们的类的子类访问。

改写代码:

class Test    
  def method_public
    puts  "In method_public" 
  end
  
  def method_protected
    puts "In method_protected"     
  end  
  
  def method_private
    puts "In method_private"   
  end
 
  protected :method_protected
  private   :method_private
  
  def call_method_protected(testmember)
    puts testmember.method_protected
  end
  def call_method_private(testmember)
    puts testmember.method_private
  end
  
end

test1 = Test.new
test2 = Test.new
分别尝试:

       test2.call_method_protected(test1)

       输出:

       In method_protected
       nil

       test2.call_method_private(test1)

       输出:test.rb:21:in `call_method_private': private method `method_private' called for #<Test:0x36c5af4> (NoMethodError)

可知:protected方法可以被其他的实例对象访问,而private方法只能被自己的实例对象访问。


总结一下


public方法可以被定义它的类子类访问,并可以被类和子类的实例对象调用;

protected方法可以被定义它的类子类访问,不能被类和子类的实例对象调用,但可以被该类的实例对象(所有)访问;

private方法可以被定义它的类子类访问,不能被类和子类的实例对象调用,且实例对象只能访问自己的private方法。


以上的陈述中,请注意“调用”和“访问”的区别。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值