zoukankan      html  css  js  c++  java
  • Java Access Levels(访问控制)

    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

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





  • 相关阅读:
    02-02:springboot 整合filter
    02-01:springboot整合servlet开发
    01-SpringBoot项目:helloworld
    SpringBoot
    JavaScript面试题
    vue的生命周期
    小程序下的兼容性问题
    短短几行css代码实现滚动条效果
    Apache服务器的安装和配置
    闭包
  • 原文地址:https://www.cnblogs.com/cosmoseeker/p/5422266.html
Copyright © 2011-2022 走看看