簡體   English   中英

如何增加最終的 integer 變量?

[英]How can I increase the final integer variable?

Eclipse 提供final但我無法增加i變量。

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

在此處輸入圖像描述

這里最簡單的解決方案是創建一個 class:

public class FinalCounter {

    private int val;

    public FinalCounter(int intialVal) {
        val=intialVal;
    }
    public void increment(){
        val++;
    }
    public void decrement(){
        val--;
    }
    public int getVal(){
        return val;
    }

    public static void main(String[] arg){
        final FinalCounter test = new FinalCounter(0);
        test.increment(); // 1
        test.increment(); // 2
        test.increment(); // 3
        test.increment(); // 4
        test.increment(); // 5
        test.decrement(); // 4
        System.out.println(test.getVal());  // prints 4
    }
}

final 是初始化后無法更改的實體。

決賽(Java)

您可以做的是在 do/while 循環的 scope 中創建一個變量,該變量以i為最終值,並將其發送到 function。

我認為可以創建變量i的本地副本。 嘗試這個:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        final int localCopy = i; // Create here a final copy of i
        try {
            new Thread(new Runnable() {

                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            // use here the copy
                            tv.setText(Integer.toString(localCopy));
                        }
                    });
                }
            }).start(); // Don't forget to start the Thread!
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

通過創建最終的本地副本:

  • 編譯器不會再抱怨了
  • 由於 Java 按值復制,您只會增加i而不是localCopy

我想你也想啟動線程......

編輯:的確,你是對的。 您必須在循環內創建本地最終副本。 檢查新代碼。

最終變量只能在定義時初始化一次,不一定。 它可以在構造函數中的任何時間設置,但只能設置一次。 在您使用 i++ 遞增 i 的情況下,您試圖將遞增的值再次分配給 i ,這是不允許的。

我所做的是添加:

private int i;

在這之前:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

之后,您將能夠像往常一樣使用您的變量,而不必將其標記為最終變量。

您可以像這樣創建一個計數器 class 並增加它。 這樣,計數器 object 的引用可能是最終的,但您仍然可以設置它的值嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM