What is the hook method?
Hooks are used to hang things, as the name implies. So to hang something must have a hanging thing, or the hoop, or the edge of the wall. So to be able to hang things must have a hook of the hoop, to a hook. So in Java the same principle, you first need a hanging thing, a mount of things.
How to implement Hooks
is an empty implementation of an abstract method or method defined in an interface, in practice, for example, there is an interface, there are 7 methods in this interface, and you only want to use one of the methods, then you can write an abstract class to implement this interface, In this abstract class, the method you want to use is set to abstract, the other method to empty implementation, and then you inherit this abstract class, there is no need to implement other methods, this is the role of the hook method.
Abstract Parent class
publicabstractclass AbstractClass { publicabstractbooleanisOpen(); publicfinalvoidoperating() { if(isOpen()) { System.out.println("钩子方法开启"); }else { System.out.println("钩子方法关闭"); } }}
Implementation class
publicclassextends AbstractClass { //钩子方法能挂在到operating能干预到operating业务逻辑 @Override publicbooleanisOpen() { returntrue; } publicstaticvoidmain(String[] args) { newAchieveClass(); ac.operating(); }}
You can intervene in the business process of the parent method as long as you rewrite isopen. The equivalent of IsOpen is mounted in the parent class's operating ().
The Hooks method in Java