MainPresenter 实现类

/**
* presenter 层,承担业务逻辑处理,数据源处理等
*/
public class MainPresenter extends BasePresenter<MainContract.IMainView, DataModel> implements MainContract.IMainPresenter {

@Override
public void handlerData() {
getView().showDialog();

getModel().requestBaidu(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}

@Override
public void onResponse(Call call, Response response) throws IOException {
String content = response.body().string();
getView().succes(content);
}
});
}

@Override
public void detach() {
super.detach();
/**
* 释放内存、关闭网络请求、关闭线程等操作
*/
Log.d("==========", "detech: 解除绑定,释放内存");
}
}
    这里我们传入的是 DataModel 的实例,而 MainPresenter 类是继承自 BasePresenter 基类的,所以在 BasePresenter 的反射代码中的 this 指的就是 MainPresenter 类的对象。看如下反射代码:

反射代码部分:

//通过获得泛型类的父类,拿到泛型的接口类实例,通过反射来实例化 model
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();

Log.d("===========", "attach: "+this.getClass().getSimpleName());
if (type != null) {
Type[] types = type.getActualTypeArguments(http://www.amjmh.com/v/BIBRGZ_558768/);
try {
mModel = (M) ((Class<?>) types[1]).newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
--------------------- 

原文地址:https://www.cnblogs.com/hyhy904/p/11299090.html