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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值