Java modified class

First, the modified class visit

Public access modifier -public: visible to all classes, use objects: classes, interfaces, variables, methods, all public methods and variables of the class can be inherited by subclasses.

public  static  void main (String [] args) {
     // main method must use public modified, or does not run such Java interpreter 
}

 

-Protected protected access modifiers: a base class and subclasses in the same package: the package can be accessed in any other class; subclass of the base class and not in the same package: subclass instance inherited from the base class can be accessed over the protected method, and can not access the protected base class instance method. May be modified data members, constructors, methods members, not modified within the class category except; using the object: variable method.

-Default default access modifiers: visible, without any modifiers in the same package, using objects: classes, interfaces, variables, methods.

// declare variables and methods can be used without any modifiers 
String Version = "1.3.0" ;
 Boolean the processOrder () {
     return   to true ; 
}

-Private private access modifiers: all classes and subclasses can be seen in the same package, using the object: variables, methods, classes can not be modified except within the class;

public  class Logger { 

    // private variables, and other types can not be directly set the value of the variable, the variable to be operated by a method 
    Private String format; 
    
    public String getFormat () {
         return  the this .format; 
    } // return value format 

    public  void the setFormat (String format) {
         the this .format = format; 
    } // set the value of format 
}

 

 

Second, access to non-modified class

static modifier: Modified class methods, class variables, static methods used to separate the object of the statement, static variables (class variables), regardless of how many objects of a class instance, it is only one copy of a static variable. Local variables can not be declared as static variables; static methods can not use non-static class variable.

Class variables and class methods may be used to access and classname.methodname classname.variablename

public class InstanceCounter{
    private static int numInstances = 0;//静态变量

    protected static int getCount(){
        return numInstances;
    }//静态方法

    private static void addInstance(){
        numInstances++;
    }

    InstanceCounter(){
        InstanceCounter.addInstance();
    }

    public static void main(String[] args){
        System.out.println(InstanceCounter.getCount());
        for(int i=0;i<500;++i){
            new InstanceCounter();
        }
         System.out.println(InstanceCounter.getCount());
    }
}

operation result:

0
500

 

The final modifier: Modified-classes, methods, variables, the modified final class can not be inherited, inherited methods can not be redefined classes (i.e. inherited by subclasses, quilts not modified), variable constant can not be modified

 

abstract modifier: Create abstract classes and abstract methods

Abstract class can not instantiate objects, the sole purpose of the abstract class is declared for the future expansion of the class, if an abstract class contains methods must then this class is an abstract class; however abstract class can contain abstract methods.

Abstract method is no way to achieve the specific implementation is provided by the sub-class; all abstract methods of the parent class inherit any subclass of abstract class to achieve unless the subclass is also abstract class.

abstract public  class Caravan {
     Private String Model;
     Private String year;
     public  abstract  void GOFAST (); // abstract method 
} 

class CaravanClass the extends Caravan {
  // implement the abstract methods
  void GOFAST () {
    // ......
  }
}

 

synchronized modifier: synchronized keyword to declare the method at the same time can only be accessed by the same thread

transient modifier: When a serialized object contains instance variables transient modification, Java Virtual Machine (JVM) skipping the variable

public  transient  int limit = 33 is; // not persistent 
public  int I; // persistence

The volatile modifier: volatile modified member variable at each thread to access, are forced to re-read the value of the variable from shared memory. When members of the variable changes, a thread is forced to change the value to shared memory, so that two different threads will see the value of a member variable of the same

public  class MyRunnable the implements the Runnable {
     Private  volatile  Boolean Active; 

    public  void RUN () { 
        actiive = to true ;
         the while (Active) { // // ...... 
        } 
    } 

    public  void STOP () { 
        Active = to false ; // 
    } 
} 
/ * 
* usually thread calls a run () method, another thread calls stop () method, the active value if ① buffer is used, the loop ② when the active is false is not stopped. 
* But the modified experiment volatile active, so the loop stops ② of active is false.
*/

 

Guess you like

Origin www.cnblogs.com/jaci/p/11373458.html