8.3final keyword

final concept is described in the program of the end connector, the final keyword in the java following functions: defining the class can not be inherited, the method definition can not be overwritten, define constants (global constants).
Example: Using fianl defined class can not have subclasses

final class Channel{}   //这个类不能有子类

Channel category due to the use of the final keyword, so the class does not allow subclasses, in fact, the String class also used the final definition, so the String class can not define a subclass.

When a subclass inherits the parent class method can actually override the parent class, but do not want one of your methods be overridden, can be defined using a final
example: the use of final methods can not be defined subclasses override

class  a{
   public  final  void  count() {} //方法不允许被子类覆写
}

class  b extends a{
public void count(){}   //[错误] 该方法无法被覆写
}

count a parent class () method uses the final keyword is defined such that the method can not be overridden in a subclass.

In some system design, it may use 1 to turn the status, 0 means closed state. If the direct operation of 0 or 1, is likely to cause confusion state, in this case can be expressed by a number of names is 0 or 1. There is an important technology in the final keyword in; you can make use of its definition constant, constant content can not be modified once it is defined.

Example: Use constants defined final

public class channel1 {
       private final int ON=1;   //常量ON表示数字1,状态为打开。
       private final int OFF=0;  //常量OFF表示数字0,状态为关闭
}

Constant definition when needed to set the corresponding contents, and the contents can not be changed once defined, in order to distinguish between ordinary members with constant properties java program, it requires constant names all uppercase letters.

Most system designs, as some global constants will often use markers, so during constant definitions, often will use public static final composition to define global constants.
Example: Defining Global Constants

public class channel1 {
       public static final int ON=1; //全局常量ON表示数字1,状态为打开
       public static final int OFF=1;  //全局常量OFF表示数字0,状态为关闭
}

static main function is to define a common data while using the final after a defined constants represent public constants in the actual development of the project will make use of this structure define the relevant status code.

Tip: For a constant and string concatenation

Example: global constant connection with string

public class channel1 {
	
	public static final String INFO="mldn";
	public static void main(String[] args) {
		String strA="www.mldn.cn";
		String strB="www."+INFO+".cn";//常量连接
		System.out.println(strA==strB);
	}
}
true

This program implements an INFO constant string concatenation, two end result is a String class point to the same heap memory.

Published 162 original articles · won praise 9 · views 3116

Guess you like

Origin blog.csdn.net/ll_j_21/article/details/104399220