Visibility of fields, methods, classes in JAVA

  1. Polymorphism is special when it comes to domains. I can't understand that the Chinese version of the book is directly called domain. I read the original English version, and the original version is fields. Although the direct translation is correct, the variable in question is not the domain. Specifically checked what is the meaning of field in java? Many people think of it as a range enclosed by curly braces. In fact, there is also a thing in the framework - domain model, which is also called domain, domain model.

     

    then find this article

    What is a field in java?

     

    A field is an attribute. A field may be a class's variable, an object's variable, an object's method's variable, or a parameter of a function.

    [java]  view plain copy  
     
    1. class bike{   
    2.   static int bikes;  
    3.   int gear;  
    4.   int cadence;  
    5.   
    6.   void create( int newGear, int newCadence ){  
    7.     bikes = bikes + 1;  
    8.     gear = newGear;  
    9.     cadence = newCadence;}  
    10.   int getSpeed(){  
    11.     int speed = gear*cadence*5*3.141;  
    12.     return speed;  
    13.   }  
    14. }  

    'bikes' is a class's variable (class variable) (static field).
    'gear' and 'cadence' could be an object's variables (instance variables) (non-static fields).
    'speed' is an object's method's variable (local variable). 
    'newGear' and 'newCadence' are parameters of a function (parameters).

     

     

    field is an attribute that can be a class variable, an object variable, an object method variable or a function parameter. (Additionally, class's variables, class instance variables and static variables are called class's variables, generic variables, also known as class variables or data fields, in fact, they can also be translated into attributes, class attributes, it doesn't sound weird, from Baidu encyclopedia).

     

     

    [java]  view plain copy  
     
    1. class bike{   
    2.   static int bikes;  
    3.   int gear;  
    4.   int cadence;  
    5.   
    6.   void create( int newGear, int newCadence ){  
    7.     bikes = bikes + 1;  
    8.     gear = newGear;  
    9.     cadence = newCadence;}  
    10.   int getSpeed(){  
    11.     int speed = gear*cadence*5*3.141;  
    12.     return speed;  
    13.   }  
    14. }  

     


    bikes is a class variable (static field).

    gear and cadence are object variables (instance variables) (non-static fields).

    (There is a little contradiction here. In fact, according to Encyclopedia, bikes, gear, and cadence are all class variables, bikes are static variables in class variables, and gear and cadence are instance variables in class variables.)

     

    speed is a variable (local variable) of the object method.

    (See no, local variable, java does not have global variable, global variable, if you want to say, the scope of the class variable is the same as the global variable, but it is not called that).

     

    newGear and newCadence are parameters (parameters) of a function (method).

     

  2. ** 
  3.  * public class, visible in all packages 
  4.  * uthorauthor OOS 
  5.  * 
  6.  */  
  7. public class PublicClass {  
  8.     /** 
  9.      * public domain, visible in all subclasses 
  10.      */  
  11.     public int publicNum;  
  12.       
  13.     /** 
  14.      * Private field, visible to the current class 
  15.      */  
  16.     private int privateNum;  
  17.       
  18.     /** 
  19.      * Protected domain, visible in all subclasses 
  20.      */  
  21.     protected int protectedNum;  
  22.       
  23.     /** 
  24.      * Default domain, visible in subclasses of the current package 
  25.      */  
  26.     int defaultNum;  
  27.       
  28.     /** 
  29.      * public method 
  30.      * Available: All classes 
  31.      * Inheritable: all subclasses 
  32.      */  
  33.     public void publicMethod(){};  
  34.       
  35.     /** 
  36.      * private method 
  37.      * Available: current class 
  38.      * Inheritable: none 
  39.      */  
  40.     private void privateMethod(){};  
  41.       
  42.     /** 
  43.      * Protection method 
  44.      * Available in: current package 
  45.      * Inheritable: all subclasses 
  46.      */  
  47.     protected void protectedMethod(){};  
  48.       
  49.     /** 
  50.      * default method 
  51.      * Available in: current package 
  52.      * Inheritable: subclasses of the current package 
  53.      */  
  54.     void defaultMethod(){};  
  55.       
  56.     /** 
  57.      * Private class, only visible in the class in which it is defined 
  58.      * uthorauthor OOS 
  59.      * 
  60.      */  
  61.     private class PrivateClass{  
  62.           
  63.     }  
  64.       
  65.     /** 
  66.      * Protected class, visible only in current package, current class and all subclasses 
  67.      * uthorauthor OOS 
  68.      * 
  69.      */  
  70.     protected class ProtectedClass{  
  71.         protected ProtectedClass()  
  72.         {  
  73.               
  74.         }  
  75.     }  
  76.       
  77.     /** 
  78.      * Default class, only visible in the current package 
  79.      * uthorauthor OOS 
  80.      * 
  81.      */  
  82.     class DefaultClass {  
  83.   
  84.     }  
  85. }  

 

 

The test class CurrentPackageTest in the same package as PublicClass is as follows:

 

[java]  view plain copy
 
 
 
  1. public class CurrentPackageTest extends PublicClass{  
  2.     public static void main(String[] args)  
  3.     {  
  4.         PublicClass pc = new PublicClass();  
  5.         //The current package can use protected methods and default methods  
  6.         pc.protectedMethod();  
  7.         pc.defaultMethod();  
  8.         //The protected class is visible in the current package  
  9.         PublicClass.ProtectedClass protectedClass = new PublicClass().new ProtectedClass();  
  10.         //The default class is visible in the current package  
  11.         DefaultClass defaultClass = new PublicClass().new DefaultClass();  
  12.     }  
  13.       
  14.     public CurrentPackageTest()  
  15.     {  
  16.         System.out.println(super.publicNum);  
  17.         System.out.println(super.protectedNum);  
  18.         //PublicClass and the current class belong to the same package, so defaultNum can be inherited  
  19.         System.out.println(super.defaultNum);  
  20.     }  
  21. }  


Different from PublicClass, the test class OtherPackageTest in a package is as follows:

 

 

[java]  view plain copy
 
 
 
    1. public class OtherPackageTest extends PublicClass{  
    2.     public static void main(String[] args)  
    3.     {  
    4.         PublicClass pc = new PublicClass();  
    5.         //PublicClass does not belong to the same package as the current class, only public methods can be used  
    6.         pc.publicMethod();  
    7.     }  
    8.       
    9.     public OtherPackageTest()  
    10.     {  
    11.         System.out.println(super.publicNum);  
    12.         System.out.println(super.protectedNum);  
    13.     }  
    14. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325698896&siteId=291194637