我正在閱讀Bruce Eckel編寫的「Thinking in Java」一書,並且我遇到了一個代碼片斷,我或多或少都能理解,但想要修改。實際上,代碼的目的是顯示類靜態變量僅在調用所述類的構造函數方法之前實例化一次。是否可以將參數傳遞給Java構造方法?如果是這樣,這是一個好習慣嗎?
這裏是鏈接到本書的上repl.it 代碼(我加的註釋):https://repl.it/Bhct/6,我會後和低於其結果是:
class Main {
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.c1.f(99);
}
// Want to initialize a counter int here
// static int counter = 1;
// Want to pass counter to Cups constructor here:
// static Cups x = new Cups(counter);
static Cups x = new Cups();
// counter++;
// static Cups x = new Cups(counter);
static Cups y = new Cups();
// counter++;
// static Cups x = new Cups(counter);
static Cups z = new Cups();
}
class Cup {
Cup(int marker) {
System.out.println("Cup("+ marker +")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Cups {
int counter = 1;
static Cup c1;
static Cup c2;
static {
c1 = new Cup(1);
c2 = new Cup(2);
}
// Want to pass an arg to Cups() like this:
// Cups(int counter) {
// System.out.println("Cups() constructor #" + counter);
// }
Cups() {
System.out.println("Cups()");
}
}
結果
Cup(1)
Cup(2)
Cups()
Cups()
Cups()
Inside main()
f(99)
我想要做的是編輯Cups()
構造函數的日誌,以包含一個表示它們被調用順序的計數器,即:
Cup(1)
Cup(2)
Cups() 1
Cups() 2
Cups() 3
Inside main()
f(99)
請參閱我的意見,我如何認爲這可以完成。在main中定義靜態變量並通過調用counter++
對其進行增量操作不起作用,因爲預計會出現「」。但是,我以爲我早些時候宣佈計數器爲int?
我已經嘗試了一些這樣的變化,就像在主內部和外部增加一個方法,但我沒有運氣。
我在這裏錯過了什麼主要的Java概念?
對於這個問題的措辭,我很抱歉。不完全確定如何提出這個問題。
您可以創建一個靜態類計數器或一個靜態int(要打印它,只需將其與String.valueOf(int值)一起轉換。 – kunpapa