Java中接口以及接口回调的简单使用

2020-01-25  本文已影响0人  伪装的狼

public class InterfaceDemo {

public static void main(String[] args){
    sendHttpRequests("你好,孤独的人!", new HttpRequests() {
        @Override
        public void success(String response) {
            System.out.println("访问成功了呢!");
        }

        @Override
        public void fail() {
            System.out.println("访问失败哦!");
        }
    });
}

public static void sendHttpRequests(String address, HttpRequests httpRequests){
    if (!address.isEmpty()){
        httpRequests.success(address);
    }else {
        httpRequests.fail();
    }
}

}

interface HttpRequests {
void success(String response);
void fail();
}

上一篇下一篇

猜你喜欢

热点阅读