1. Final concept
Final can modify variables or methods. In some cases, the programmer may have the following requirements:
A, when you do not want a method of the parent class to overwrite (override) The quilt class, you can use the final keyword to decorate.
b, when you do not want the value of a variable of a class to be modified, you can use the final decoration. If a variable is final, the initial value must be assigned, or a compilation error occurs.
C, when you do not want the class to be inherited, you can use the final decoration .
//use of the final method [Demo128.java] Public classDemo128 { Public Static voidMain (string[] args) {AAA AAA=NewAaa (); Aaa.show (); BBB BBB=NewBBB (); Bbb.show (); }}classaaa{inta=0;//If a does not assign an initial value, a is 0. Value should be assigned after defining the type//PI does not allow modification//a variable with a final decoration should be named with an underscore to differentiate the representation. This is the standard for Java programmers. Final floatreate_1=3.1415926f;//use final to ensure that data that needs to be forced to be unmodified must be locked with final//final int b; //when you use final to define a variable, you must assign an initial value or an error. //b=1; Final Public voidSendmes () {//using final to modify a member method means that it cannot be modified and cannot be overwritten. SYSTEM.OUT.PRINTLN ("Send Message"); } Public voidShow () {System.out.println ("A=" +a); }}Final classBbbextendsaaa{//defining a class before and final means that the class is not allowed to be inherited PublicBbb () {a++; //reate_1=reate+1; } /*Public void Sendmes () {System.out.println ("Send Message")}*/}
final-- Precautions
1. The final modified variable is also called constant, which is usually named by Xx_xx_xx. (underlined)
2. When the final modified variable is defined, it must be assigned and cannot be assigned later.
final-- when do you use
1, because of case considerations, a method of the class does not allow modification.
2. Classes are not inherited by other classes.
3, some variable values are fixed, such as Pi 3.1415926