1. 程式人生 > >不同權限訪問詳細細節

不同權限訪問詳細細節

source pub 分享 span stat 調用 imp img public

技術分享圖片

 1 package com.package1;
 2 
 3 /**
 4  * 程序執行入口和調用方法在不同類但在同一個包中,除了private方法,其他任何權限的方法都可以都可相互調用
 5  * @author Administrator
 6  *
 7  */
 8 public class Source {
 9     public static void main(String[] args) {
10         SamePackage samePackage = new SamePackage();
11         System.out.println("================XX void====================");
12 samePackage.method1(); 13 samePackage.method2(); 14 samePackage.method4(); 15 System.out.println("================XX static void===================="); 16 samePackage.method11(); 17 samePackage.method21(); 18 samePackage.method41(); 19 System.out.println("================XX static String public(protected(default))====================");
20 samePackage.method111(samePackage.method211(samePackage.method411("qqqqqqqqqq"))); 21 System.out.println("================XX static String default(protected(public))===================="); 22 samePackage.method411(samePackage.method211(samePackage.method111("qqqqqqqqqq")));
23 } 24 }
 1 執行結果:
 2 ================XX void====================
 3 SamePackage method1!
 4 SamePackage method2!
 5 SamePackage method4!
 6 ================XX static void====================
 7 SamePackage method11!
 8 SamePackage method21!
 9 SamePackage method41!
10 ================XX static String public(protected(default))====================
11 SamePackage method411!
12 SamePackage method211!
13 SamePackage method111!
14 ================XX static String default(protected(public))====================
15 SamePackage method111!
16 SamePackage method211!
17 SamePackage method411!
 1 package com.package1;
 2 
 3 public class SamePackage {
 4     public void method1(){
 5         System.out.println("SamePackage method1!");
 6     }
 7     
 8     protected void method2(){
 9         System.out.println("SamePackage method2!");
10     }
11     
12     private static void method3(){
13         System.out.println("SamePackage method3!");
14     }
15     
16     void method4(){
17         System.out.println("SamePackage method4!");
18     }
19 
20     public static void method11(){
21         System.out.println("SamePackage method11!");
22     }
23     
24     protected static void method21(){
25         System.out.println("SamePackage method21!");
26     }
27     
28     private static void method31(){
29         System.out.println("SamePackage method31!");
30     }
31     
32     static void method41(){
33         System.out.println("SamePackage method41!");
34     }
35     
36     public static String method111(String str){
37         System.out.println("SamePackage method111!");
38         return str;
39     }
40     
41     protected static String method211(String str){
42         System.out.println("SamePackage method211!");
43         return str;
44     }
45     
46     private static String method311(String str){
47         System.out.println("SamePackage method311!");
48         return str;
49     }
50     
51     static String method411(String str){
52         System.out.println("SamePackage method411!");
53         return str;
54     }
55     
56     /**
57      * 程序執行入口和調用方法在同一個類中,任何權限的方法都可以都可相互調用
58      * @param args
59      */
60     public static void main(String[] args) {
61         SamePackage samePackage = new SamePackage();
62         System.out.println("================XX void====================");
63         samePackage.method1();
64         samePackage.method2();
65         samePackage.method3();
66         samePackage.method4();
67         System.out.println("================XX static void====================");
68         samePackage.method11();
69         samePackage.method21();
70         samePackage.method31();
71         samePackage.method41();
72         System.out.println("================XX static String default(private(protected(public)))====================");
73         samePackage.method411(samePackage.method311(samePackage.method211(samePackage.method111("wwwwwww"))));
74         System.out.println("================XX static String public(protected(private(default)))====================");
75         samePackage.method111(samePackage.method211(samePackage.method311(samePackage.method411("qqqqqqqqqq"))));
76     }
77 }
 1 執行結果:
 2 ================XX void====================
 3 SamePackage method1!
 4 SamePackage method2!
 5 SamePackage method3!
 6 SamePackage method4!
 7 ================XX static void====================
 8 SamePackage method11!
 9 SamePackage method21!
10 SamePackage method31!
11 SamePackage method41!
12 ================XX static String default(private(protected(public)))====================
13 SamePackage method111!
14 SamePackage method211!
15 SamePackage method311!
16 SamePackage method411!
17 ================XX static String public(protected(private(default)))====================
18 SamePackage method411!
19 SamePackage method311!
20 SamePackage method211!
21 SamePackage method111!
 1 package com.package2;
 2 
 3 import com.package1.SamePackage;
 4 
 5 /**
 6  * 程序執行入口和調用方法在不同包中,僅public權限的方法可以調用
 7  * @author Administrator
 8  *
 9  */
10 public class DifferentPackage {
11     public static void main(String[] args) {
12         SamePackage samePackage = new SamePackage();
13         System.out.println("================XX void====================");
14         samePackage.method1();
15         System.out.println("================XX static void====================");
16         samePackage.method11();
17     }
18 }
1 執行結果:
2 ================XX void====================
3 SamePackage method1!
4 ================XX static void====================
5 SamePackage method11!

不同權限訪問詳細細節