gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:05:26 24 4
gpt4 key购买 nike

抽象类中没有定义抽象方法。必须在子类中定义它们的主体。

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

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 类的抽象方法和一个具体方法 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方法中没有编写任何与线程相关的逻辑。

最佳答案

InBackGround如何在后台线程上运行(参见@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的源代码,了解更多信息。

关于java - 抽象如何隐藏 OOP 中的主要功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57025407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com