Java内部抽象类的匿名类初始化

news/2025/2/26 7:08:44

      说在前面的话,以前写过一次这个变态代码,后来重构,把那个new的语法简化了,最近又要整,差点都想不起来了,留个文档把

1、下面这个案例更变态,抽象类还有一个个泛型类:首先内部抽象类的定义:

  1. /* 
  2.  * 
  3.  * Created by stone 
  4.  * 
  5.  * 
  6.  */  
  7. package net.stoneapp.quanshi.qsand.application;  
  8.   
  9. import android.app.Application;  
  10. import android.util.Log;  
  11. import android.widget.Toast;  
  12.   
  13. import com.alibaba.fastjson.JSON;  
  14. import com.alibaba.fastjson.JSONObject;  
  15.   
  16. import net.stoneapp.quanshi.qsand.utils.AndUtils;  
  17.   
  18. import cn.jpush.android.api.JPushInterface;  
  19. import retrofit2.Call;  
  20. import retrofit2.Response;  
  21. import rx.Subscriber;  
  22.   
  23. /** 
  24.  * Created by dengel on 15/12/28. 
  25.  */  
  26. public class QsandApplication extends Application {  
  27.     AndUtils mAndUtils;  
  28.   
  29.     @Override  
  30.     public void onCreate() {  
  31.         super.onCreate();  
  32.         mAndUtils = new AndUtils(this);  
  33.         JPushInterface.setDebugMode(true);  
  34.         JPushInterface.init(this);  
  35.     }  
  36.   
  37.     public AndUtils getAndUtils(){  
  38.         return mAndUtils;  
  39.     }  
  40.     public abstract class JSONSubscriber extends Subscriber<JSONObject> {  
  41.         @Override  
  42.         public void onCompleted() {  
  43.   
  44.         }  
  45.   
  46.         @Override  
  47.         public void onError(Throwable e) {  
  48.             if(e.getMessage()!=null) {  
  49.                 Log.i("Subscriber Error", e.getMessage());  
  50.                 Toast.makeText(QsandApplication.this, e.getMessage(), Toast.LENGTH_LONG).show();  
  51.             }  
  52.             e.printStackTrace();  
  53.   
  54.         }  
  55.   
  56.     }  
  57.     public abstract class Callback<T> implements retrofit2.Callback<T>{  
  58.         @Override  
  59.         public void onResponse(Call<T> call, Response<T> response) {  
  60.             int status = response.code();  
  61.             if(status>=400 && status<500){  
  62.                 JSONObject jsonObj =  (JSONObject)JSON.parse(response.errorBody().toString());  
  63.                 Toast.makeText(QsandApplication.this,jsonObj.getString("detail"),Toast.LENGTH_SHORT).show();  
  64.             }  
  65.         }  
  66.   
  67.         @Override  
  68.         public void onFailure(Call<T> call,Throwable t) {  
  69.   
  70.             if(t.getMessage()!=null) {  
  71.                 Log.e("Callback Error:", t.getMessage());  
  72.                 Toast.makeText(QsandApplication.this, t.getMessage(), Toast.LENGTH_LONG).show();  
  73.             }  
  74.             t.printStackTrace();  
  75.   
  76.         }  
  77.   
  78.     }  
  79. }  

 

 

2、匿名类初始化方法:

 

    1. public void postVerifyCode(View view){  
    2.         mMobilelView.setError(null);  
    3.         String mobile = mMobilelView.getText().toString();  
    4.         boolean cancel = false;  
    5.         View focusView = null;  
    6.         if (TextUtils.isEmpty(mobile) || !StringUtils.isMobileNumber(mobile)) {  
    7.             mMobilelView.setError(getString(R.string.error_invalid_mobile));  
    8.             focusView = mMobilelView;  
    9.             cancel = true;  
    10.         }  
    11.         if (cancel){  
    12.             focusView.requestFocus();  
    13.   
    14.   
    15.         }else {  
    16.             showProgress(true);  
    17.             Services.AuthService service = Services.getRetrofit().create(Services.AuthService.class);  
    18.             User user = new User();  
    19.             user.setMobile(mobile);  
    20.             final QsandApplication qsandApplication = (QsandApplication)getApplication();  
    21.             service.postVerifyCode(user).enqueue(qsandApplication.new Callback<User>(){//重点就在这句new的语法  
    22.                 @Override  
    23.                 public void onResponse(Call<User> call, Response<User> response) {  
    24.   
    25.                 }  
    26.   
    27.   
    28.   
    29.   
    30.             });  
    31.         }  
    32.   
    33.   
    34.     }  

http://www.niftyadmin.cn/n/712240.html

相关文章

matebook14支持触摸屏吗_2020款的matebook14增加了多点触控屏是最大亮点

华为matebook14终于获得了Windows的授权&#xff0c;而且更新十代酷睿处理器&#xff0c;更加值得一提的是&#xff0c;MateBook13/14版新增了触屏这一操作黑科技&#xff0c;也就是说&#xff0c;我们以后用的电脑也可以像手机一样&#xff0c;直接用手触屏操作。比如我们想放…

简单邮箱源码java_简单的java实现发送电子邮件

这里实现用Java发送电子邮件&#xff0c;可以发送简单文本&#xff0c;或者html&#xff0c;再或者就是带附件了。首先就是需要两个包&#xff0c;我用的是apache中commons中的&#xff0c;下载地址http://commons.apache.org/可以去下载。需要的两个包为&#xff1a;mail.jar、…

Word2007插入两种页码

做毕设&#xff0c;摘要&#xff0c;Abstract&#xff0c;目录&#xff0c;第一章&#xff0c;现在想要“摘要&#xff0c;Abstract&#xff0c;目录”编页码“为罗马数字&#xff0c;第一章开始为阿拉伯数字&#xff0c;可以按如下步骤&#xff1a; 1.各部分插入分页符&#x…

docker制作镜像

1.下载镜像&#xff1a; docker pull centos:7.2.1511 2.查看本地有哪些镜像 docker images 3.启动docker docker run -ti 8686(image ID即镜像Id) 4.改变镜像 yum install vim 5.退出镜像 6.提交镜像 docker commit -m "modify vim" xxx_image 7.此时docker image可…

ts获取服务器数据_ionic4中数据交互get post jsonp请求服务器数据

ionic4.x中请求数据和angular中几乎是一样的&#xff0c;其中get、post和和服务器交互使用的是HttpClientModule模块。下面我们看看ionic4中数据交互get post jsonp请求服务器数据。一、ionic4.x get 请求数据Angular5.x以后get、post和和服务器交互使用的是HttpClientModule模…

从安全和成本来看企业混合云的演变

爆炸性的数据让企业考虑混合云存储来满足容量需求。但云的成本和安全问题需要解决。 各种规模的企业都面临非结构化数据集前所未有的增长速度。 增长的速度和使用的容量主要是源于捕捉和传输数据的物联网设备的快速部署。 许多小型企业正寻求公共云来帮助他们跟上这种爆炸性增长…

java32位怎么用eclipse_32位eclipse怎么安装?

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言&#xff0c;它只是一个框架和一组服务&#xff0c;用于通过插件组件构建开发环境。下面我们就来看一下如何安装32位的Eclipse&#xff1a;1、打开浏览器&#xff0c;用百度搜索引擎搜索关键字“eclipse”&…

新RS CCIEv5,新V5 Written笔试400-101

在YESLAB全员讲师通过一个通宵的共同奋战&#xff0c;于第一个考试日一举拿下RS CCIEv5 新written-400-101First of the world!!转载于:https://blog.51cto.com/enderjoe/1422842