抽象如何隐藏OOP中的主要功能

时间:2019-07-14 07:29:16

标签: java android abstraction

抽象类中未定义抽象方法。必须在子类中定义其主体。

现在假设我们正在处理Asynctask,并且在此类中有一个抽象方法
  doInBackground(Params ... params)

public class InsertAsyncTask extends AsyncTask<Note, Void, Void> {

private NoteDao mNoteDao;

public InsertAsyncTask(NoteDao dao) {
    mNoteDao = dao;
}

@Override
protected Void doInBackground(Note... notes) { 
    mNoteDao.insertNotes(notes); // How this function becomes thread save( goes in background) ?
    return null;
}

这是AsyncTask类的Abstract方法和onPreExcute()的一种具体方法。

/**
 * Override this method to perform a computation on a background thread. The
 * specified parameters are the parameters passed to {@link #execute}
 * by the caller of this task.
 *
 * This method can call {@link #publishProgress} to publish updates
 * on the UI thread.
 *
 * @param params The parameters of the task.
 *
 * @return A result, defined by the subclass of this task.
 *
 * @see #onPreExecute()
 * @see #onPostExecute
 * @see #publishProgress
 */
@WorkerThread
protected abstract Result doInBackground(Params... params);
/**
 * Runs on the UI thread before {@link #doInBackground}.
 *
 * @see #onPostExecute
 * @see #doInBackground
 */
@MainThread
protected void onPreExecute() {
}

/**
 * <p>Runs on the UI thread after {@link #doInBackground}. The
 * specified result is the value returned by {@link #doInBackground}.</p>
 * 
 * <p>This method won't be invoked if the task was cancelled.</p>
 *
 * @param result The result of the operation computed by {@link #doInBackground}.
 *
 * @see #onPreExecute
 * @see #doInBackground
 * @see #onCancelled(Object) 
 */

现在的问题是,使它们异步的隐藏实现在哪里(我的意思是doinbackground抽象方法实际上是如何使事情在后台工作的?作为其抽象方法,直到在子类中实现之前它都是空的。对吗?) 。由于抽象方法仅在子类中定义,因此我们没有在override方法中编写与线程相关的任何逻辑。

1 个答案:

答案 0 :(得分:0)

doInBackGround如何在后台线程上运行(请参阅@WorkerThread):

    /**
 * Override this method to perform a computation on a background thread. The
 * specified parameters are the parameters passed to {@link #execute}
 * by the caller of this task.
 *
 * This method can call {@link #publishProgress} to publish updates
 * on the UI thread.
 *
 * @param params The parameters of the task.
 *
 * @return A result, defined by the subclass of this task.
 *
 * @see #onPreExecute()
 * @see #onPostExecute
 * @see #publishProgress
 */
@WorkerThread
protected abstract Result doInBackground(Params... params);

现在由WorkerRunnable运行

        mWorker = new WorkerRunnable<Params, Result>() {
        public Result call() throws Exception {
            mTaskInvoked.set(true);
            Result result = null;
            try {
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                //noinspection unchecked
                result = doInBackground(mParams);
                Binder.flushPendingCommands();
            } catch (Throwable tr) {
                mCancelled.set(true);
                throw tr;
            } finally {
                postResult(result);
            }
            return result;
        }
    };

您可以通过按住Control键并单击AsynkTask关键字查看AsyncTask的源代码,以了解更多信息。