必承其重 | 欲带皇冠 2008-10-18 19:53 采纳率: 50%
浏览 1463
已采纳

在 Java 中,public、 protected、 package-private 和 private 之间有什么区别?

In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public, protected and private, while making class and interface and dealing with inheritance?

转载于:https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in

  • 写回答

24条回答 默认 最新

  • Didn"t forge 2008-10-18 19:57
    关注

    The official tutorial may be of some use to you.

                │ Class │ Package │ Subclass │ Subclass │ World
                │       │         │(same pkg)│(diff pkg)│ 
    ────────────┼───────┼─────────┼──────────┼──────────┼────────
    public      │   +   │    +    │    +     │     +    │   +     
    ────────────┼───────┼─────────┼──────────┼──────────┼────────
    protected   │   +   │    +    │    +     │     +    │         
    ────────────┼───────┼─────────┼──────────┼──────────┼────────
    no modifier │   +   │    +    │    +     │          │    
    ────────────┼───────┼─────────┼──────────┼──────────┼────────
    private     │   +   │         │          │          │    
    
    + : accessible
    blank : not accessible
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(23条)

报告相同问题?