码迷,mamicode.com
首页 > 数据库 > 详细

Java Access Levels(访问控制)

时间:2016-04-22 18:14:39      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

Access Levels

ModifierClassPackageSubclassWorld
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

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
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

原来我一直都弄反了
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

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





Java Access Levels(访问控制)

标签:

原文地址:http://www.cnblogs.com/cosmoseeker/p/5422266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!