Java接口回调代码展示

        在我们的开发过程中,Java回调是我们经常使用到的,比如我们的Thread的内部封装,还有我们的网络异步请求回调等等功能都有使用到我们的Java回调,本文主要记录一下Java回调的开发过程。

  1. Demo目录结构为:

    IWeatherUtils 定义接口
    Main 运行演示
    Weather 实体类
    WeatherUtils 实现接口工具类
  2. 代码展示:
  • IWeatherUtils:
    package com.clover.InterfaceCallBack;
    
    public interface IWeatherUtils {
    
        void getWeather(String city, CallBack callBack);
    
        interface CallBack {
            void success(Weather weather);
    
            void error(String msg);
        }
    }
    
  • Main:

    package com.clover.InterfaceCallBack;
    
    public class Main {
    
        public static void main(String[] args) {
    
            WeatherUtils weatherUtils = new WeatherUtils();
            weatherUtils.getWeather("北京", new IWeatherUtils.CallBack() {
                @Override
                public void success(Weather weather) {
                    System.out.println(weather.getMsg());
                }
    
                @Override
                public void error(String msg) {
                    System.out.println(msg);
                }
            });
    
        }
    }
    
  • Weather:
    package com.clover.InterfaceCallBack;
    
    public class Weather {
    
        //城市
        private String city;
        //天气信息
        private String msg;
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    
  • WeatherUtils:
    package com.clover.InterfaceCallBack;
    
    public class WeatherUtils implements IWeatherUtils {
    
        @Override
        public void getWeather(String city, CallBack callBack) {
    
            Weather weather = new Weather();
            weather.setCity(city);
            switch (city) {
                case "北京": {
                    String msg = city + "今天雾霾严重,空气质量差,建议在家刷剧玩游戏!";
                    weather.setMsg(msg);
                    callBack.success(weather);
                    break;
                }
                case "广州": {
                    String msg = city + "今天晴朗,空气质量优,适合出行!";
                    weather.setMsg(msg);
                    callBack.success(weather);
                    break;
                }
                case "上海": {
                    String msg = city + "今天有大暴雨,建议出门带伞!";
                    weather.setMsg(msg);
                    callBack.success(weather);
                    break;
                }
                case "深圳": {
                    String msg = city + "今天高温,建议在家吹空调!";
                    weather.setMsg(msg);
                    callBack.success(weather);
                    break;
                }
                default: {
                    callBack.error(city + "天气获取失败!");
                }
            }
        }
    }
    

    https://github.com/LeeckyNTG/JavaCode.git

猜你喜欢

转载自blog.csdn.net/qq_28695593/article/details/92962717