public static int do_dispatch(){
{
int prior = this.getPriority();
ThreadCB threadToDispatch=null;
ThreadCB runningThread=null;
TaskCB runningTask=null;
try {
runningTask = MMU.getPTBR().getTask();
runningThread = runningTask.getCurrentThread();
} catch(NullPointerException e) {}
// If necessary, remove current thread from processor and reschedule it.
if(runningThread != null)
{
// Check if quantum is exceeded
if (HTimer.get() < 1)
{
//Increment the priority to lower priority value
prior++;
this.setPriority(prior);
}
//Append to expired
expired[prior].append(this);
runningTask.setCurrentThread(null);
MMU.setPTBR(null);
runningThread.setStatus(ThreadReady);
readyQueue.append(runningThread);
class Sub {
static int y;
public static void foo() {
this.y = 10;
}
}
我試圖編譯我的計劃,但我繼續使用得到的錯誤在這種情況下這個關鍵字。我明白'this'代表調用該方法的對象,並且靜態方法不會綁定到任何對象。
實施'this'的正確方法是什麼?
你不能「執行」這個''。 [在語言規範中定義](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.8.3),這是編譯時錯誤在靜態方法中使用它。 –
'this'指向類的當前實例,並且在靜態上下文中不存在實例 - 所有方法和屬性都處於類級別(又名 - 它們在類的所有實例之間共享)。爲了處理你在代碼中顯示的方法的靜態屬性,你可以用類名前綴它,比如'Sub.y = 10'。請注意,您不需要在此處執行此操作,因爲您沒有隱藏任何現有屬性。即:'y = 10'應該足夠了。 –
**請勿自行放置自己的帖子。**您的編輯使您的問題難以理解。 – EJP