Private
Private functions can only be called in the context of the class and subclass, and can only be accessed through self.
This means that the private function can only be accessed within the object.
The access permission of the object instance variable (@) is private.CopyCodeThe Code is as follows: Class accesstest
Def Test
Return "Test private"
End
Def test_other (other)
"Other Object" + other. Test
End
End
T1 = accesstest. New
T2 = accesstest. New
P t1.test # => test private
P t1.test _ Other (T2) # => Other Object test private
# Now make 'test' private
Class accesstest
PRIVATE: Test
End
P t1.test _ Other (T2) # error in 'test _ other ': Private method 'test' called for # <accesstest: 0x292c14> (nomethoderror)
Protected
The protect function can only be called in the context of the class and subclass, but can be in the form of other_object.function. (This is equivalent to the private mode of C ++)
The key is that the protected function can be used inside other objects of the same type (including subclasses.
# Now make 'test' protect
Class accesstest
Protected: Test
End
P t1.test _ Other (T2) # Other Object test private
Public
The public function can be called anywhere. The default access permission for member functions and constants is public.