不同权限访问详细细节
Posted celineluo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不同权限访问详细细节相关的知识,希望对你有一定的参考价值。
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!
以上是关于不同权限访问详细细节的主要内容,如果未能解决你的问题,请参考以下文章