final keyword
0. Preface
January 3, 2021 15:03:29
1. The use of final can also be added to the class; Because sometimes we write it ourselves, we don't need the next to customize it! The toolkit can also be implemented in this way;
2. The naming of variables is standardized, but I don't think it is necessary to name the corresponding operations in the basic package;
3. Look at other people's code and learn to use other people's standard operation! We only know how to use it, but we don't know why to design it;
1. Act on class
No subclasses. Specific example: String type
/** * @Description CollectionUtils From: org springframework. util. CollectionUtils * @Author liguang * @Date 2022/01/03/23:37 */ public final class CollectionUtil extends CollectionUtils { }
Here you can customize the class and use the final keyword to attribute! It means that class inheritance is prohibited
2. Act on the method
Subclasses cannot be overridden, but they can be inherited and used.
The following is a function to be used when learning the template mode:
Abstract parent class:
/** * @Description Game Abstract parent * @Author liguang * @Date 2021/12/26/18:17 */ public abstract class Game { private void login(){ System.out.println("Start logging in to the game..........."); } /** * The hook function determines whether to switch modes */ public abstract Boolean hook(); /** * There are many scene modes to switch */ public abstract void switchModel(); private void play(){ System.out.println("Start playing games..........."); } private void end(){ System.out.println("game over............."); } /** * Template mode. Add the final keyword to prohibit subclasses from overriding methods * The pipeline process has been defined here! You only need subclasses to write according to specific patterns */ public final void startGame(){ login(); if (hook()){ switchModel(); } play(); end(); } }
Although subclasses cannot override the startGame method, subclasses can access the startGame method.
Look at several subclasses:
/** * @Description Dungeon game * @Author liguang * @Date 2021/12/26/18:23 */ public class DXC extends Game{ /** * Hook function, indicating whether to switch game mode * @return */ @Override public Boolean hook() { return Boolean.FALSE; } @Override public void switchModel() { } }
/** * @Description LOL game * @Author liguang * @Date 2021/12/26/18:22 */ public class LOL extends Game{ /** * Hook function, indicating whether to switch game mode * @return */ @Override public Boolean hook() { return Boolean.FALSE; } @Override public void switchModel() { System.out.println("Play games normally............"); } }
/** * @Description King glory game * @Author liguang * @Date 2021/12/26/18:21 */ public class WZRX extends Game{ /** * Hook function, indicating whether to switch game mode * @return */ @Override public Boolean hook() { return Boolean.TRUE; } @Override public void switchModel() { System.out.println("Switch to entertainment mode......................"); } }
Let's look at the test class:
@Test public void testGetBeanForType() { final Map<String, Game> beanNameForValueMap = GlobalSpringApplicationContext.getBeanForType(Game.class); beanNameForValueMap.forEach((k,v)->{ v.startGame(); System.out.println("+++++++++++++++++++"); }); }
Then you can see the output of each subclass:
Start logging in to the game........... Start playing games........... game over............. +++++++++++++++++++ Start logging in to the game........... Start playing games........... game over............. +++++++++++++++++++ Start logging in to the game........... Switch to entertainment mode...................... Start playing games........... game over............. +++++++++++++++++++
A set of pipeline engineering is defined, and the subclass rewriting process only needs to be implemented according to its own needs.
3. Acts on variables and member properties
3.1. Acting on variables
public void forEach(final Person person){ // xxxxxxx }
When the operation in the method body is prohibited, the address value of person is changed. Because it is possible to re create a person object to overwrite the original value of person. This source code often has.
3.2. Function on member variables
This is often seen in spring
@Controller @AllArgsConstructor public class UserController { private final UserService userService; }
You can see the member variable decorated with final, which can be written as follows. There are three specific ways to use
3.2.1 direct assignment
Assign values directly later:
public class Animal { private final String str = "hello"; }
Once modified, the address value of the object cannot be modified, but the attribute value of the object can be changed.
3.2.2 static initialization
Use static initialization
public class Animal { private static final String str; static { str = ""; } }
However, this method requires that the member variable should be decorated with the static+final keyword.
3.2.3 parametric structure
Injection using parametric construction method:
public class Animal { private final String str; public Animal(String str) { this.str = str; } }
As you can see, the construction must contain the methods of member variables decorated with final. You can see that the variables defined by final must be initialized before they can be used.
So here's a question:
public class Animal { private final String str = "hello"; public Animal(String str) { this.str = str; } }
Look at the test. What is the final str? However, it is found that when you write in this way, idea directly reports compile time exceptions.
Cannot assign a value to final variable 'str'
It shows that after the member variable modified by final is directly assigned, it will not participate in the construction; However, if there is no assignment, it must be in the parameter constructor.