Java Access Levels(访问控制)

Access Levels

ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

ModifierAlphaBetaAlphasubGamma
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN

原来我一直都弄反了
protected的可见性实际上是大于package的.实习面试的时候,我一直都说错了。

Access Levels的实质

If you have a field that’s private it means no other class can get at it. Wrong! If you really want to you can subvert the access control mechanisms in almost any language. Usually the way through is via reflection. The rationale is that debuggers and other system tools often need to see private data , so usually the reflection interfaces allow you to do this.
C++ doesn’t have this kind of reflection, but there you can just use direct memory manipulation since C++ is fundamentally open memory.
The point of access control is not to prevent access, but more to signal that the class prefers to keep some things to itself. Using access modifiers, like so many things in programming, is primarily about communication.
http://martinfowler.com/bliki/AccessModifier.html

果然是专家,说出来的话太有水平了!
我理解的,访问控制实际是编译时有效的,类似于宏定义一样的东西,并不具备运行时检查。
不知道这个理解是不是错误的。我暂时还没找到去哪里验证这个观点。希望有人能给出切实的说法。





转载于:https://www.cnblogs.com/cosmoseeker/p/5422266.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值