JAVA (2) of the class about access control

  • Four access rights of the members of the class
    • private
      • In the current class can only access
    • No modification
      • The same class package can access
    • protected
      • The same package can access the class
      • Subclasses can be accessed in different packages
    • public
      • All classes can access
  • Sample Code
    • The same package
      •  
        . 1  Package com.study.main;
         // package with non-child class 

        . 3 public class Two . 4 { . 5 Private int A; . 6 protected int B; . 7 public int C; . 8 int D; . 9 }

         

      •  
        Package com.study.main;
         // same bun class 
        public  class Son the extends the Main 
        { 
        
        }

         

      •  1 package com.study.main;
         2 //测试类
         3 import com.study.one.One;
         4 import com.study.one.SonOne;
         5 
         6 public class Main
         7 {
         8     private int a;
         9     protected int b;
        10     public int c;
        11     int d;
        12 
        13     public static void main(String[] args)
        14     {
        15         Main m = new Main();// same class 
        16          System.out.println ( "same class" );
         . 17          System.out.println (mA); // Private 
        18 is          System.out.println (MB); // protected 
        . 19          System.out.println ( MC); // public 
        20 is          System.out.println (MD); // no modification 
        21 is          
        22 is          son = S new new son (); // subclasses in the same package 
        23 is          System.out.println ( "the same package subclass " );
         24-  //         System.out.println (SA); // Private inaccessible 
        25         System.out.println (SB); // protected 
        26 is          System.out.println (SC); // public 
        27          System.out.println (SD); // unmodified 
        28          
        29          Two T = new new Two (); / / same class Feizi package 
        30          System.out.println ( "non-child class in the same package" );
         31 is  //         System.out.println (TA); // Private inaccessible 
        32          System.out.println ( TB); // protected 
        33 is          System.out.println (TC); // public 
        34 is          System.out.println (TD);// No Modifier 
        35          
        36          Sonone = SO new new Sonone (); // subclasses different packages 
        37 [          System.out.println ( "subclasses different packages" );
         38 is  //         System.out.println (SO. A); // Private inaccessible 
        39          System.out.println (so.b); // protected 
        40          System.out.println (so.c); // public
         41 is  //         System.out.println (so.d ); // no modification was unable to access 
        42 is          
        43 is          
        44 is          One = O new new One (); // different packages of non-child class 
        45         System.out.println ( "non-child class in a different package" );
         46 is  //         System.out.println (OA); // Private unable to access
         47  //         System.out.println (OB); // protected not access 
        48          System.out.println (OC); // public
         49  //         System.out.println (OD); // unmodified inaccessible 
        50          
        51 is          
        52 is      }
         53 }

         

    • Different packages
      •   
        Package com.study.one;
         // different packet classes Feizi 
        public  class One 
        { 
            Private  int A;
             protected  int B;
             public  int C;
              int D; 
        }

         

      • . 1  Package com.study.one;
         2  // different classes buns 
        . 3  Import com.study.main.Main;
         . 4  
        . 5  public  class Sonone the extends the Main
         . 6  {
         . 7  
        . 8 }

         

    • Run shot

      •   
  • to sum up
    • The current class can access
    • A package of the same class can not access the private addition
    • Different subclasses package can access only public and protected
    • Different packages of non-child class can access only public
    •  

       

       

Guess you like

Origin www.cnblogs.com/quxiangjia/p/11911421.html