不同package存取類別的Protected資料成員
package A; public class Base{ private int privateData=0; protected int protectedData=1; int packageData=2; public int publicData=3; public Base(){ } public void publicMethod(){ System.out.format("publicMethod called%n"); } private void privateMethod(){ System.out.format("privateMethod called%n"); } void packageMethod(){ System.out.format("packageMethod called%n"); } protected void protectedMethod(){ System.out.format("protectedMethod called%n"); } } |
package B; Subclass s=new Subclass(); s.publicMethod();//legal //System.out.format("privateData: %2d%n",s.privateData);//illegal private變數不能繼承 |
存取權限開放的大小
public > protected > package(default) > private
class的權限如未被開啟,則裡面的資料就算是public也不能被存取。
引用自http://life.csu.edu.au/java-tut/java/javaOO/accesscontrol.html
留言列表