JavaSE-10.4.2【权限修饰符】

 1 package day1.yppah04.p3;
 2 
 3 /*
 4 4.3 权限修饰符
 5 
 6 修饰符     同一类     同一包中子类/无关类      不同包的子类      不同包的无关类
 7 private     OK
 8 默认        OK              OK
 9 protected   OK              OK                     OK
10 public      OK              OK                     OK              OK
11  */
12 
13 public class Fu {
14 
15     private void show1(){
16         System.out.println("private");
17     }
18 
19     void show2(){
20         System.out.println("默认");
21     }
22 
23     protected void show3(){
24         System.out.println("protected");
25     }
26 
27     public void show4(){
28         System.out.println("public");
29     }
30 
31     public static void main(String[] args) {
32         Fu f = new Fu();
33         f.show1();
34         f.show2();
35         f.show3();
36         f.show4();
37     }
38 
39 }
 1 package day1.yppah04.p3;
 2 
 3 public class Zi extends Fu{
 4 
 5     public static void main(String[] args) {
 6         Zi z = new Zi();
 7         z.show2();
 8         z.show3();
 9         z.show4();
10         //z不可访问show1()
11     }
12 
13 }
 1 package day1.yppah04.p3;
 2 
 3 public class Demo {
 4 
 5     public static void main(String[] args) {
 6         Fu f = new Fu();
 7         f.show2();
 8         f.show3();
 9         f.show4();
10         //f不可访问show1()
11     }
12 
13 }
 1 package day1.yppah04.p4;
 2 
 3 import day1.yppah04.p3.Fu;
 4 
 5 public class Zi extends Fu {
 6 
 7     public static void main(String[] args) {
 8         Zi z = new Zi();
 9         z.show3();
10         z.show4();
11         //z不可访问show1()show2()
12     }
13 
14 }
 1 package day1.yppah04.p4;
 2 
 3 import day1.yppah04.p3.Fu;
 4 
 5 public class Demo {
 6     public static void main(String[] args) {
 7         Fu f = new Fu();
 8         f.show4();
 9         //f不可访问show1()show2()show3()
10     }
11 }

 

 

posted @ 2021-05-27 20:35  yub4by  阅读(46)  评论(0编辑  收藏  举报