在Kotlin中定义Jave抽象类的对象

时间:2019-05-03 12:04:23

标签: android kotlin abstract-class

我在Java中有此类

public abstract class SimpleApiCallback<T> implements ApiCallback<T> {

    private static final String LOG_TAG = "SimpleApiCallback";

    private Activity mActivity;

    private Context mContext = null;
    private View mPostView = null;

    /**
     * Failure callback to pass on failures to.
     */
    private ApiFailureCallback failureCallback = null;

    /**
     * Constructor
     */
    public SimpleApiCallback() {
    }

    /**
     * Constructor
     *
     * @param activity The context.
     */
    public SimpleApiCallback(Activity activity) {
        mActivity = activity;
    }

    /**
     * Constructor
     *
     * @param context    The context.
     * @param postOnView the view to post the code to execute
     */
    public SimpleApiCallback(Context context, View postOnView) {
        mContext = context;
        mPostView = postOnView;
    }

    /**
     * Constructor to delegate failure callback to another object. This allows us to stack failure callback implementations
     * in a decorator-type approach.
     *
     * @param failureCallback the failure callback implementation to delegate to
     */
    public SimpleApiCallback(ApiFailureCallback failureCallback) {
        this.failureCallback = failureCallback;
    }

    private void displayToast(final String message) {
        if (null != mActivity) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
                }
            });
        } else if ((null != mContext) && (null != mPostView)) {
            mPostView.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onNetworkError(Exception e) {
        if (failureCallback != null) {
            try {
                failureCallback.onNetworkError(e);
            } catch (Exception exception) {
                Log.e(LOG_TAG, "## onNetworkError() failed" + exception.getMessage(), exception);
            }
        } else {
            displayToast("Network Error");
        }
    }

    @Override
    public void onMatrixError(final MatrixError e) {
        if (failureCallback != null) {
            try {
                failureCallback.onMatrixError(e);
            } catch (Exception exception) {
                Log.e(LOG_TAG, "## onMatrixError() failed" + exception.getMessage(), exception);
            }
        } else {
            displayToast("Matrix Error : " + e.getLocalizedMessage());
        }
    }

    @Override
    public void onUnexpectedError(final Exception e) {
        if (failureCallback != null) {
            try {
                failureCallback.onUnexpectedError(e);
            } catch (Exception exception) {
                Log.e(LOG_TAG, "## onUnexpectedError() failed" + exception.getMessage(), exception);
            }
        } else {
            displayToast(e.getLocalizedMessage());
        }
    }
}

我可以在Java中这样称呼

new SimpleApiCallback<Void>(this) {
            @Override
            public void onSuccess(Void avoid) {
               ///
            }

            @Override
            public void onNetworkError(Exception e) {
                ///
            }

            @Override
            public void onUnexpectedError(Exception e) {
                ///
            }

            @Override
            public void onMatrixError(MatrixError e) {
            //
            }
        }

但是在kotlin中,我尝试了多种格式来调用它,但是它不起作用

喜欢

val callback = object : SimpleApiCallback<>(activity){
            fun onSuccess(avoid: Void) {
                ///
            }

            fun onNetworkError(e: Exception) {
                ///
            }

            fun onUnexpectedError(e: Exception) {
                ///
            }

            fun onMatrixError(e: MatrixError) {
                //
            }
        }

有人可以建议吗?

1 个答案:

答案 0 :(得分:0)

由于您使用的是Java.lang.Void,因此您仍然可以在Kotlin中使用它,因为它与函数的声明void不同。

val callback = object : SimpleApiCallback<Void>(activity) {
    fun onSuccess(avoid: Void) {
        ///
    }

    override fun onNetworkError(e: Exception) {
        ///
    }

    override fun onUnexpectedError(e: Exception) {
        ///
    }

    override fun onMatrixError(e: MatrixError) {
        //
    }
}

上面的方法应该起作用,但是如果您的onSuccess()不属于SimpleApiCallback,那么您也应该覆盖它。