ruby学习笔记1

  • If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object---it is never possible to access another object's private methods directly, even if the object is of the same class as the caller.
  • Ruby differs from other OO languages in another important way. Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.
  • A class's initialize method is automatically declared to be private.
  • 在不同对象实例当中的作用域不同
    Java当中,不管private还是protected的方法,都可以被同一个类别的其他对象实例调用;
    Ruby当中,private方法不能被同一个类别的其他对象实例调用;
  • 在类继承关系当中的作用域不同
    ruby当中,不管private还是protected方法,都可以被子类继承;
    Java当中,private方法不能被子类继承;
  • 这种方法调用的差异,也许来自于不同的面向对象理念:Java的面向对象来自C++,强调类继承关系,所以方法调用限定类继承的层次结构当中的作用域,却 不强调对象实例作用域;而ruby的面向对象来自smalltalk,即使是类别也是对象,因此方法调用作用域都是针对对象实例调用来设置的。

For Java--

public:所有类可见。
pirvate:只有同一类内部的方法可见,在有就是内部类也可以访问到。
默认(friendly):包内可见。
protected:继承可见。

 

 

你可能感兴趣的:(c,OO,Access,Ruby,smalltalk)