1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package com.***.inner; public class BaseBean {
private class InnerClass{ private String innerPrivateStr; String innerDefaultStr; protected String innerProtectedStr; public String innerPublicStr;
public InnerClass(){ innerPrivateStr = "innerPrivateStr"; innerDefaultStr = "innerDefaultStr"; innerProtectedStr = "innerProtectedStr"; innerPublicStr = "innerPublicStr"; } }
private String privateStr; String defaultStr; protected String protectedStr; public String publicStr;
public BaseBean(){ privateStr = "privateStr"; defaultStr = "defaultStr"; protectedStr = "protectedStr"; publicStr = "publicStr"; }
public void show(){ InnerClass innerClass = new InnerClass(); System.out.println("BaseBean:InnerClass: " + innerClass.innerPrivateStr); System.out.println("BaseBean:InnerClass: " + innerClass.innerDefaultStr); System.out.println("BaseBean:InnerClass: " + innerClass.innerProtectedStr); System.out.println("BaseBean:InnerClass: " + innerClass.innerPublicStr);
System.out.println("BaseBean: " + privateStr); System.out.println("BaseBean: " + defaultStr); System.out.println("BaseBean: " + protectedStr); System.out.println("BaseBean: " + publicStr); } }
package com.***.inner; public class SubBean extends BaseBean {
public void show(){ System.out.println("SubBean: " + defaultStr); System.out.println("SubBean: " + protectedStr); System.out.println("SubBean: " + publicStr); }
}
package com.***.outer; public class OuterPackageSubBean extends BaseBean{
public void show(){ System.out.println("OuterPackageSubBean: " + protectedStr); System.out.println("OuterPackageSubBean: " + publicStr); }
}
package com.***.test; public class TestAccessControl { BaseBean baseBean; SubBean subBean; OuterPackageSubBean outerPackageSubBean;
public TestAccessControl(){ baseBean = new BaseBean(); subBean = new SubBean(); outerPackageSubBean = new OuterPackageSubBean(); }
public void show(){ System.out.println("********The Same Class.########"); baseBean.show(); System.out.println("********The Sub Class in the same package.########"); subBean.show(); System.out.println("********The Sub Class in other packages.########"); outerPackageSubBean.show(); System.out.println("********The World.########"); System.out.println("TestAccessControl: baseBean " + baseBean.publicStr); System.out.println("TestAccessControl: subBean " + subBean.publicStr); System.out.println("TestAccessControl: outerPackageSubBean " + outerPackageSubBean.publicStr); }
public static void main(String[] args) { TestAccessControl testAccessControl = new TestAccessControl(); testAccessControl.show(); } }
|