1. 程式人生 > >Java內部抽象類的匿名類初始化

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) {

                }




            });
        }


    }