public class Learning {
int i = 1; // i is assigned to 1.
static int s = 2;// s is assigned to 2.
Learning() {
i = 0; s++; // i is assigned to 0 and s increases to 2.
System.out.println("test: " + i + s);// this line will display test: 03 since the values are above given.
}
public static void main (String [] args) {
Learning t1 = new Learning();// Creating and instance variable from the Class Learning.
System.out.println("t1: " + t1.i + t1.s);//t1 reaches with the help of the . the value of i as well as s and again this line will print out t1: 03
t1.s = 6;// Now the variable get s new value which is 6
Learning t2 = new Learning();// Creating another instance variable from the class called Learning
t2.i = 8;// Now the value for i is set for 8.
System.out.println("t2: " + t2.i + t2.s);// Now I thought the program would display t2: 86 since I have already got the value 8 and 6 assigned above, but it doesn't do that.
}
}
好的傢伙,如上所見我有一個新的Java代碼,我認爲總的來說,我確實瞭解很多東西,至少我做了以上評論,我知道我的思維方式是正確的。任務02:此Java代碼如何工作?
隨意和檢查我上面的意見,糾正我,如果我錯了。
我已經測試和上面的代碼實際打印如下:
test: 03
t1: 03
test: 07
t2: 87
因此,換句話說,我是正確的部分,我只是不明白爲什麼它在打印測試:07,因爲沒有循環,爲什麼t2:87不是t2:86?
我最初希望這樣的事情:
test: 03
t1: 03
t2: 86
任何想檢查PROFIS?
在此先感謝。
我建議你學習的靜態和成員變量之間的區別。 –
使用調試器學習的時間 –
@ Code-Guru,我通過例子學到了最好的東西,希望能有一些知識豐富,體面的用戶可以幫助我完成這個代碼 – Rosehip