看板 java 關於我們 聯絡資訊
public class test { static int c; public static void main(String[] args) { int a = 1; int b = 2; //new test(a, b); System.out.printf("兩數字相加 %d, %d 答案為 %d %n ", a, b, c); } public test(int a, int b){ c =a + b; } } 去掉不必要得部份,這樣執行會得到 兩數字相加 1, 2 答案為 0 把註解去掉,會得到 兩數字相加 1, 2 答案為 3 雖然物件建構子當作方法用很怪,但是功能還是正常的 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.85.140
jodoken:建構值當方法用很奇怪嗎? 03/14 18:34
LaPass:非常奇怪.... 03/14 19:28
mars90226:建構子就是在你需要那個物件時才呼叫阿~ 03/14 22:15