Java中有兩個級別的訪問控制:
(1)At the top level — public, or package-private (no explicit modifier)
(2)At the member level — public, private, protected, or package-private (no explicit modifier)
At the top level:類(class)聲明時,訪問修飾符有兩種:public,不使用訪問修飾符。被public修飾的類可以所有的類訪問;沒有訪問修飾符的類只能被其所在的包(package)中的類訪問。
At the member level:成員(member)聲明時,訪問修飾符有四種選擇:public、private、protected、不使用訪問修飾符。被public修飾的成員在所有的類中都可以被訪問;被private修飾的成員只能在其所在的類中被訪問;被protected修飾的成員,可以被同包中的類以及在其它包中的該成員所屬類的子類訪問;沒有修飾符的成員可以被同包中的類訪問。
成員級的訪問控制:
