Ruby与JAVA访问控制的区别
上一篇 / 下一篇 2009-09-03 16:02:36 / 个人分类:JAVA
x"cg\^&vI*@0Ruby语言中,访问修饰符有三个:public、protected和private。
p6?$TE&skt/['?8^+j051Testing软件测试网F]/SO d4f
Ruby指定方法访问权限的方式有两种,一种是在定义方法之前声明方法的访问权限,另一种是在方法定义后指定访问权限。51Testing软件测试网 NIv)cJ5J q
51Testing软件测试网)zc)M!@7_n-IG P
代码段1:
- class Sup
- private
- def method1
- puts "method1"
- end
- protected
- def method2
- puts "method2"
- end
- public
- def method3
- puts "method3"
- end
- end
class Sup private def method1 puts "method1" end protected def method2 puts "method2" end public def method3 puts "method3" end end51Testing软件测试网)Ha dZ.uV6M)~)O1p
:vr4Q7?^.?'y P0代码段2:51Testing软件测试网K2}
jIJ1]
- class Sup
- def method1
- puts "method1"
- end
- def method2
- puts "method2"
- end
- def method3
- puts "method3"
- end
- private :method1
- protected :method2
- public :method3
- end
class Sup def method1 puts "method1" end def method2 puts "method2" end def method3 puts "method3" end private :method1 protected :method2 public :method3 end
v!\e&Fj l:UR0
t3nkKC3bFk0代码段1中,访问修饰符不能与方法名写在同一行,比如这样写,运行时就会报错:51Testing软件测试网P|g5C0E)uF
- private def method1
private def method1
0RtVW4O'P@051Testing软件测试网 kY_?JF
_{
在Ruby中,换行就代表一句代码的结束,封号可以省略,也就是说这里的private已经是一句完整的代码了,如果非得放在同一行,这样写也是没错的,但没什么意义:51Testing软件测试网{,T
Xy1Pk xl
- private; def method1
private; def method151Testing软件测试网)__W9g]Ko j"S
fA2k]aqx0Ruby中,方法的访问权限默认是public的(initialize除外,它默认是private的)。51Testing软件测试网zZa2i/]m1` Ju
\o#~
代码段1中的声明方法,一个访问修饰符修饰之后的所有方法,直到下一个访问修饰符出现,如:
9@"w$Gw2}%Pj$Ob0代码段3:
- class Sup
- private
- def method1
- puts "method1"
- end
- def method4
- puts "method4"
- end
- protected
- def method2
- puts "method2"
- end
- public
- def method3
- puts "method3"
- end
- end
class Sup private def method1 puts "method1" end def method4 puts "method4" end protected def method2 puts "method2" end public def method3 puts "method3" end end
u%uL!q i,`csH'O
|S0
7p9~h*}3V8R W9G0这里在method1之后添加了一个method4,这个method4也是private的。但在代码段2中,如果往method1之后添加一个method4,这个method4仍然是public(默认)的。
|q;h"FF051Testing软件测试网)t3dV;@z"m$]
在Ruby中:51Testing软件测试网X(~4U$wXNu c[1\c
GlH8m#NSLD\0o}0protected(保护)方法只能被定义了该方法的类或其子类的对象所调用。整个家族均可访问。51Testing软件测试网/d6tb,L-\ f
private(私有)方法不能被明确的接收者调用,其接收者只能是self。这意味着私有方法只能在当前对象的上下文中被调用:你不能调用另一个对象的私有方法。
51Testing软件测试网
|'P)Ox!C,jWY
Ruby中:
Emj}.~.Z!`4S0代码段4:51Testing软件测试网"p5ak}KK7o8\
- class Sup
- private
- def method1
- puts "method1"
- end
- end
- class Sub < Sup
- def method_1
- method1 #这样的代码是合法的
- end
- end
class Sup private def method1 puts "method1" end end class Sub < Sup def method_1 method1 #这样的代码是合法的 end end51Testing软件测试网#Ky_ tP u q
51Testing软件测试网X"H&pTQ{2xV
这样的代码是合法的,但在Java中:
3dZ;d5D,y0代码段5:51Testing软件测试网A3R$GR:x4fC
n
- class Sup{
- private void method1(){
- System.out.println("method1");
- }
- }
- class Sub extends Sup{
- public void method_1(){
- method1();
- super.method1();//这2种调用方式都是不合法的,子类不继承父类的private方法,也不能通过super关键字调用父类的private方法。
- }
- }
class Sup{ private void method1(){ System.out.println("method1"); } } class Sub extends Sup{ public void method_1(){ method1(); super.method1();//这2种调用方式都是不合法的,子类不继承父类的private方法,也不能通过super关键字调用父类的private方法。 } }
y}Q?X6jV|
GQ051Testing软件测试网\0]*N5e9o"tb!v
这2种调用方式都是不合法的,子类不继承父类的private方法,也不能通过super关键字调用父类的private方法。
9M c8{:l _+@
j0Ruby中:
)c ~X9{UdQ0代码段6:
- class Sup
- def method2
- puts "method2"
- end
- protected :method2
- end
- class Sub < Sup
- def sup_method2
- sup = Sup.new
- sup.method2 #这样的代码是合法的
- end
- end
class Sup def method2 puts "method2" end protected :method2 end class Sub < Sup def sup_method2 sup = Sup.new sup.method2 #这样的代码是合法的 end end51Testing软件测试网6[&R\eQw+CK
r^fMmp0这样的代码是合法的,但在Java中:
eB
p{\@@0代码段7:
- class Sup{
- protected void method2(){
- System.out.println("method1");
- }
- }
- class Sub extends Sup{
- public void sup_method2(){
- Sup sup = new Sup();
- sup.method2();//这样的代码只有当子类和父类在同一个包(package)下的时候才合法
- }
- }
class Sup{ protected void method2(){ System.out.println("method1"); } } class Sub extends Sup{ public void sup_method2(){ Sup sup = new Sup(); sup.method2();//这样的代码只有当子类和父类在同一个包(package)下的时候才合法 } }
~1w\!w"lrHQ(i0
d}cE*D2IE+A)M(Lg0这样的代码只有当子类和父类在同一个包(package)下的时候才合法。51Testing软件测试网3[kI?6f
Ruby中,private跟protected的区别在哪呢?把代码段6中,method2的访问权限改成private再运行,就报错了。也就是前面说的“private(私有)方法不能被明确的接收者调用,其接收者只能是self。”。51Testing软件测试网;k"pZ%b*BDm1X
因此private和protected的在ruby当前和Java的定义有很大的区别:51Testing软件测试网y
LcZ,nB
'tlv`
Arl01、在不同对象实例当中的作用域不同
^&PuuJqH0Java当中,不管private还是protected的方法,都可以被同一个类别的其他对象实例调用;
6?3R#e(~g0Ruby当中,private方法不能被同一个类别的其他对象实例调用;51Testing软件测试网-B/?whC$}+N(b)c]
51Testing软件测试网tM#p_&r