将变量另存为子类(接口)

时间:2019-01-18 16:12:19

标签: java oop inheritance interface multiple-inheritance

我正在制作一个AsyncTask扩展,该扩展使用不同的操作与服务进行通信,并根据所使用的操作返回不同类型的结果。每个动作都有一个侦听器,该侦听器由扩展了一个称为OnCommunicationFailedListener的接口的接口组成。问题是,我只想拥有一个变量来保存侦听器OnCommunicationFailedListener类型,并使用户能够在该变量中保存所有“子接口”。他们可以按原样保存它们,但是我不能调用“子接口”介绍的方法。

class CommunicationTask extends AsyncTask<Void, Void, String> {
    private Action action;  
    private OnCommunicationFailedListener listener;

    enum Action {
        LOGIN,
        LOGOUT,
        SEARCH
    }

    interface OnCommunicationFailedListener {
        void onCommunicationFailed();
    }

    interface OnLoginAchievedListener extends OnCommunicationFailedListener {
        void onLoggedIn(String result);
        void onLoginError(String error);
    }

    interface OnLogoutAchievedListener extends OnCommunicationFailedListener {
        void onLoggedOut(String result);
        void onLogoutError(String error);
    }

    interface OnSearchAchievedListener extends OnCommunicationFailedListener {
        void onSearchAchieved(String[] results);
    }

    @Override protected String doInBackground(Void... voids){
        try {
            // Try to communicate with a query, resulting in some result.
            return result;
        } catch(IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override protected void onPostExecute(String result){
        super.onPostExecute(result);

        if(result == null) {
            listener.onCommunicationFailed();
            return;
        }

        // ALL OF THESE GIVE ERRORS, SINCE THE METHODS DO NOT EXIST ANYMORE
        switch(action){
            case LOGIN:
                if(<check if result is ok>){
                    listener.onLoggedIn(result);
                } else listener.onLoginError(<some error>);
                break;

            case LOGOUT:
                if(<check if result is ok>){
                    listener.onLoggedOut(result);
                } else listener.onLogoutError(<some error>);
                break;

            case SEARCH:
                String[] results = <process result>;
                listener.onSearchAchieved(results);
                break;
        }
    }

    void login(String user, String password, OnLoginAchievedListener listener){
        this.listener = listener;
        action = Action.LOGIN;

        //Build the query...

        execute();
    }

    void logout(OnLogoutAchievedListener listener){
        this.listener = listener;
        action = Action.LOGOUT;

        //Build the query...

        execute();
    }

    void search(String query, OnSearchAchievedListener listener){
        this.listener = listener;
        action = Action.SEARCH;

        //Build the query...

        execute();
    }
}

这个想法是另一个类调用方法login()logout()search(),并为它们提供操作类型的侦听器。完成通信并获取结果后,将调用操作的侦听器方法(在我的代码中,这发生在switch语句中)。

问题是,由于传递的侦听器已强制转换为OnCommunicationFailedListener,因此无法调用这些方法。

有什么方法可以将子类保存在其超类类型的变量中,而不必强制转换为它?我不想为每种类型的侦听器使用实际的变量。

0 个答案:

没有答案