The Hooks method in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.