生活随笔
收集整理的這篇文章主要介紹了
Java内部抽象类的匿名类初始化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? 說在前面的話,以前寫過一次這個變態代碼,后來重構,把那個new的語法簡化了,最近又要整,差點都想不起來了,留個文檔把
1、下面這個案例更變態,抽象類還有一個個泛型類:首先內部抽象類的定義:
package?net.stoneapp.quanshi.qsand.application;????import?android.app.Application;??import?android.util.Log;??import?android.widget.Toast;????import?com.alibaba.fastjson.JSON;??import?com.alibaba.fastjson.JSONObject;????import?net.stoneapp.quanshi.qsand.utils.AndUtils;????import?cn.jpush.android.api.JPushInterface;??import?retrofit2.Call;??import?retrofit2.Response;??import?rx.Subscriber;????public?class?QsandApplication?extends?Application?{??????AndUtils?mAndUtils;????????@Override??????public?void?onCreate()?{??????????super.onCreate();??????????mAndUtils?=?new?AndUtils(this);??????????JPushInterface.setDebugMode(true);??????????JPushInterface.init(this);??????}????????public?AndUtils?getAndUtils(){??????????return?mAndUtils;??????}??????public?abstract?class?JSONSubscriber?extends?Subscriber<JSONObject>?{??????????@Override??????????public?void?onCompleted()?{????????????}????????????@Override??????????public?void?onError(Throwable?e)?{??????????????if(e.getMessage()!=null)?{??????????????????Log.i("Subscriber?Error",?e.getMessage());??????????????????Toast.makeText(QsandApplication.this,?e.getMessage(),?Toast.LENGTH_LONG).show();??????????????}??????????????e.printStackTrace();????????????}????????}??????public?abstract?class?Callback<T>?implements?retrofit2.Callback<T>{??????????@Override??????????public?void?onResponse(Call<T>?call,?Response<T>?response)?{??????????????int?status?=?response.code();??????????????if(status>=400?&&?status<500){??????????????????JSONObject?jsonObj?=??(JSONObject)JSON.parse(response.errorBody().toString());??????????????????Toast.makeText(QsandApplication.this,jsonObj.getString("detail"),Toast.LENGTH_SHORT).show();??????????????}??????????}????????????@Override??????????public?void?onFailure(Call<T>?call,Throwable?t)?{????????????????if(t.getMessage()!=null)?{??????????????????Log.e("Callback?Error:",?t.getMessage());??????????????????Toast.makeText(QsandApplication.this,?t.getMessage(),?Toast.LENGTH_LONG).show();??????????????}??????????????t.printStackTrace();????????????}????????}??}?? ?
?
2、匿名類初始化方法:
?
public?void?postVerifyCode(View?view){??????????mMobilelView.setError(null);??????????String?mobile?=?mMobilelView.getText().toString();??????????boolean?cancel?=?false;??????????View?focusView?=?null;??????????if?(TextUtils.isEmpty(mobile)?||?!StringUtils.isMobileNumber(mobile))?{??????????????mMobilelView.setError(getString(R.string.error_invalid_mobile));??????????????focusView?=?mMobilelView;??????????????cancel?=?true;??????????}??????????if?(cancel){??????????????focusView.requestFocus();??????????????}else?{??????????????showProgress(true);??????????????Services.AuthService?service?=?Services.getRetrofit().create(Services.AuthService.class);??????????????User?user?=?new?User();??????????????user.setMobile(mobile);??????????????final?QsandApplication?qsandApplication?=?(QsandApplication)getApplication();??????????????service.postVerifyCode(user).enqueue(qsandApplication.new?Callback<User>(){????????????????@Override??????????????????public?void?onResponse(Call<User>?call,?Response<User>?response)?{????????????????????}??????????????????????});??????????}??????????}??
總結
以上是生活随笔為你收集整理的Java内部抽象类的匿名类初始化的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。