protected:
和java不一样,不能被当前创建的类对象,有外部访问,但可以被传入的对象调用。
private :
不能被传入的值调用
class AccessTest def test return "test private" end def test_other(other) return "dd"+other.test end end class AccessTest private :test end p1=AccessTest.new cc=AccessTest.new puts p1.test_other(cc)
本文探讨了Ruby中的访问控制概念,包括protected和private修饰符的区别。通过具体的代码示例,展示了如何使用这些修饰符来限制类成员的访问权限,并解释了它们在实际应用中的作用。
154

被折叠的 条评论
为什么被折叠?



