• 对java中的访问限定符的理解1


    image

    可以这样理解:

    “同一类中可见”------“相对于父类,在父类的{}作用域内,父类的对象可以访问全部的成员”;

    “同一包中对子类可见”------“相对于父类,与父类在同一包A中,在包A里面的子类的{}作用域内,父类或者子类的对象访问父类成员的情况”;

    “同一包中对非子类可见”------“相对于父类,与父类在同一包A中,在包A里面的非子类的{}作用域内,父类或者子类的对象访问父类的成员的情况”;

    “不同包中对子类可见”-------“相对于父类(在包A中),与父类不在同一包(比如包B)中,在包B里面的子类的{}作用域内,子类的对象,访问父类的成员的情况”;

    “不再同一包中对非子类可见”------“相对于父类(在包A中),与父类不在同一包(比如包B)中,在包B里面的非子类的{}作用域内,父类或者子类的对象,访问父类的成员的情况”;

    下面一行一行的解释:

    一、

    image

    理解:

    “同一类中可见”------“相对于父类,在父类的{}作用域内,父类的对象可以访问全部的成员”;

     

    示例:

    image

     

    ClassA.java

     

       1:  package com.test.pkg1;
       2:   
       3:   
       4:   
       5:  public class ClassA {
       6:   
       7:      public int a;
       8:      protected int b;
       9:      int c;
      10:      private int d;
      11:      
      12:      public void fun1(){}
      13:      protected void fun2(){}
      14:      void fun3(){}
      15:      private void fun4(){}
      16:      
      17:      
      18:      public static void main(String[] args) {
      19:          
      20:          ClassA classA = new ClassA();
      21:          
      22:          classA.a = 0;
      23:          classA.b = 0;
      24:          classA.c = 0;
      25:          classA.d = 0;
      26:          
      27:          classA.fun1();
      28:          classA.fun2();
      29:          classA.fun3();
      30:          classA.fun4();
      31:          
      32:      }
      33:      
      34:      
      35:  }
      36:   

     

    编译无误!

     

    二、image

     

     

    理解:

    “同一包中对子类可见”------“相对于父类,与父类在同一包A中,在包A里面的子类的{}作用域内,父类或者子类的对象访问父类成员的情况”;

     

    image

     

    ClassA.java

     

       1:  package com.test.pkg1;
       2:   
       3:   
       4:   
       5:  public class ClassA {
       6:   
       7:      public int a;
       8:      protected int b;
       9:      int c;
      10:      private int d;
      11:      
      12:      public void fun1(){}
      13:      protected void fun2(){}
      14:      void fun3(){}
      15:      private void fun4(){}
      16:      
      17:      
      18:      public static void main(String[] args) {
      19:          
      20:          ClassA classA = new ClassA();
      21:          
      22:          classA.a = 0;
      23:          classA.b = 0;
      24:          classA.c = 0;
      25:          classA.d = 0;
      26:          
      27:          classA.fun1();
      28:          classA.fun2();
      29:          classA.fun3();
      30:          classA.fun4();
      31:          
      32:      }
      33:      
      34:      
      35:  }
      36:   
      37:  class ClassB extends ClassA{
      38:      
      39:      public void fun5()
      40:      {
      41:          ClassA classA = new ClassA();
      42:          ClassB classB = new ClassB();
      43:          
      44:          classA.a = 0;
      45:          classA.b = 0;
      46:          classA.c = 0;
      47:          classA.d = 0;
      48:          
      49:          classA.fun1();
      50:          classA.fun2();
      51:          classA.fun3();
      52:          classA.fun4();
      53:          
      54:          classB.a = 0;
      55:          classB.b = 0;
      56:          classB.c = 0;
      57:          classB.d = 0;
      58:          
      59:          classB.fun1();
      60:          classB.fun2();
      61:          classB.fun3();
      62:          classB.fun4();
      63:      }
      64:  }

    下面是系统提示:

    image

     

    三、

    image

     

     

     

    理解:

    “同一包中对非子类可见”------“相对于父类,与父类在同一包A中,在包A里面的非子类的{}作用域内,父类或者子类的对象访问父类的成员的情况”;

     

    image

     

    ClassA.java

       1:  package com.test.pkg1;
       2:   
       3:   
       4:   
       5:  public class ClassA {
       6:   
       7:      public int a;
       8:      protected int b;
       9:      int c;
      10:      private int d;
      11:      
      12:      public void fun1(){}
      13:      protected void fun2(){}
      14:      void fun3(){}
      15:      private void fun4(){}
      16:      
      17:      
      18:      public static void main(String[] args) {
      19:          
      20:          ClassA classA = new ClassA();
      21:          
      22:          classA.a = 0;
      23:          classA.b = 0;
      24:          classA.c = 0;
      25:          classA.d = 0;
      26:          
      27:          classA.fun1();
      28:          classA.fun2();
      29:          classA.fun3();
      30:          classA.fun4();
      31:          
      32:      }
      33:      
      34:      
      35:  }
      36:   
      37:  class ClassB extends ClassA{}
      38:   
      39:  class ClassC{
      40:      
      41:      public void fun5()
      42:      {
      43:          ClassA classA = new ClassA();
      44:          ClassB classB = new ClassB();
      45:          
      46:          classA.a = 0;
      47:          classA.b = 0;
      48:          classA.c = 0;
      49:          classA.d = 0;
      50:          
      51:          classA.fun1();
      52:          classA.fun2();
      53:          classA.fun3();
      54:          classA.fun4();
      55:          
      56:          classB.a = 0;
      57:          classB.b = 0;
      58:          classB.c = 0;
      59:          classB.d = 0;
      60:          
      61:          classB.fun1();
      62:          classB.fun2();
      63:          classB.fun3();
      64:          classB.fun4();
      65:      }
      66:  }

    下面是系统提示:

    image

     

    四、image

     

     

    理解:

    “不同包中对子类可见”-------“相对于父类(在包A中),与父类不在同一包(比如包B)中,在包B里面的子类的{}作用域内,子类的对象,访问父类的成员的情况”;

     

    image

     

    ClassA.java

       1:  package com.test.pkg1;
       2:   
       3:   
       4:   
       5:  public class ClassA {
       6:   
       7:      public int a;
       8:      protected int b;
       9:      int c;
      10:      private int d;
      11:      
      12:      public void fun1(){}
      13:      protected void fun2(){}
      14:      void fun3(){}
      15:      private void fun4(){}
      16:      
      17:      
      18:      public static void main(String[] args) {
      19:          
      20:          ClassA classA = new ClassA();
      21:          
      22:          classA.a = 0;
      23:          classA.b = 0;
      24:          classA.c = 0;
      25:          classA.d = 0;
      26:          
      27:          classA.fun1();
      28:          classA.fun2();
      29:          classA.fun3();
      30:          classA.fun4();
      31:          
      32:      }
      33:      
      34:      
      35:  }
      36:   

    ClassB.java

       1:  package com.test.pkg2;
       2:   
       3:  import com.test.pkg1.ClassA;
       4:   
       5:   
       6:  class ClassB extends ClassA{
       7:      
       8:      public void fun5()
       9:      {
      10:          ClassA classA = new ClassA();
      11:          ClassB classB = new ClassB();
      12:          
      13:          classA.a = 0;
      14:          classA.b = 0;
      15:          classA.c = 0;
      16:          classA.d = 0;
      17:          
      18:          classA.fun1();
      19:          classA.fun2();
      20:          classA.fun3();
      21:          classA.fun4();
      22:          
      23:          classB.a = 0;
      24:          classB.b = 0;
      25:          classB.c = 0;
      26:          classB.d = 0;
      27:          
      28:          classB.fun1();
      29:          classB.fun2();
      30:          classB.fun3();
      31:          classB.fun4();
      32:      }
      33:  }

    下面是系统提示:

    image

    只需看关于ClassB的对象的提示。

     

    五、image

     

     

    理解:

    “不再同一包中对非子类可见”------“相对于父类(在包A中),与父类不在同一包(比如包B)中,在包B里面的非子类的{}作用域内,父类或者子类的对象,访问父类的成员的情况”;

     

    image

     

    ClassA.java

       1:  package com.test.pkg1;
       2:   
       3:   
       4:   
       5:  public class ClassA {
       6:   
       7:      public int a;
       8:      protected int b;
       9:      int c;
      10:      private int d;
      11:      
      12:      public void fun1(){}
      13:      protected void fun2(){}
      14:      void fun3(){}
      15:      private void fun4(){}
      16:      
      17:      
      18:      public static void main(String[] args) {
      19:          
      20:          ClassA classA = new ClassA();
      21:          
      22:          classA.a = 0;
      23:          classA.b = 0;
      24:          classA.c = 0;
      25:          classA.d = 0;
      26:          
      27:          classA.fun1();
      28:          classA.fun2();
      29:          classA.fun3();
      30:          classA.fun4();
      31:          
      32:      }
      33:      
      34:      
      35:  }
      36:   

    ClassB.java

       1:  package com.test.pkg2;
       2:   
       3:  import com.test.pkg1.ClassA;
       4:   
       5:   
       6:  class ClassB extends ClassA{
       7:      
       8:      
       9:  }
      10:   
      11:   
      12:  class ClassC{
      13:   
      14:      public void fun5()
      15:      {
      16:          ClassA classA = new ClassA();
      17:          ClassB classB = new ClassB();
      18:          
      19:          classA.a = 0;
      20:          classA.b = 0;
      21:          classA.c = 0;
      22:          classA.d = 0;
      23:          
      24:          classA.fun1();
      25:          classA.fun2();
      26:          classA.fun3();
      27:          classA.fun4();
      28:          
      29:          classB.a = 0;
      30:          classB.b = 0;
      31:          classB.c = 0;
      32:          classB.d = 0;
      33:          
      34:          classB.fun1();
      35:          classB.fun2();
      36:          classB.fun3();
      37:          classB.fun4();
      38:      }
      39:  }

    系统提示:

    image

  • 相关阅读:
    js小案例---1.随机10不重复10数并排序2.一次输入10数并输出和
    23种设计模式-----转载
    类与类之间的关系-----转载
    设计模式六大原则-----转载
    配置JDK时环境变量path和JAVA_HOME的作用是什么?
    安装和配置JDK,并给出安装、配置JDK的步骤。
    1.Java为什么能跨平台运行?请简述原理。
    求圆的周长和面积
    java第一节课
    相关元素操作
  • 原文地址:https://www.cnblogs.com/pengdonglin137/p/2976257.html
Copyright © 2020-2023  润新知