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


          说在前面的话,以前写过一次这个变态代码,后来重构,把那个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.     }  
  • 相关阅读:
    SQL列类型
    垂直显示查询结果
    在mysql中如何写注释语句
    离开Autodesk,开启新篇章
    Autodesk 为其云技术发布新品牌- Autodesk Forge
    Using View and Data API with Meteor
    View and Data API Tips: Constrain Viewer Within a div Container
    View and Data API Tips: Hide elements in viewer completely
    View and Data API Tips : Conversion between DbId and node
    使用AxisHelper帮助理解View and Data API中的坐标系统
  • 原文地址:https://www.cnblogs.com/dhcn/p/7124905.html
Copyright © 2020-2023  润新知