日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/3/18 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java内部抽象类的匿名类初始化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? 說在前面的話,以前寫過一次這個變態代碼,后來重構,把那個new的語法簡化了,最近又要整,差點都想不起來了,留個文檔把

1、下面這個案例更變態,抽象類還有一個個泛型類:首先內部抽象類的定義:

  • /*?
  • ?*?
  • ?*?Created?by?stone?
  • ?*?
  • ?*?
  • ?*/??
  • 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;??
  • ??
  • /**?
  • ?*?Created?by?dengel?on?15/12/28.?
  • ?*/??
  • 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>(){//重點就在這句new的語法??
  • ????????????????@Override??
  • ????????????????public?void?onResponse(Call<User>?call,?Response<User>?response)?{??
  • ??
  • ????????????????}??
  • ??
  • ??
  • ??
  • ??
  • ????????????});??
  • ????????}??
  • ??
  • ??
  • ????}??
  • 總結

    以上是生活随笔為你收集整理的Java内部抽象类的匿名类初始化的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。