```java public class ExampleMethod2 { public static void test(String str, int intVal) { } public static void test31(String param, String...param2) { } private String test32(String param, String...param2) { return ""; } public static void main(String[] args) { /* * 方法的簽名(Signature)指的是方法的唯一識別符, * 它由方法的名稱和參數列表組成。簽名用於區分不同的方法, * 並確定方法的唯一性。 * * 方法的簽名包括以下兩個部分: * 方法名稱:方法的名稱用於識別方法的功能和操作。 * 參數列表:參數列表包含了方法接受的參數的數量、類型和順序。 * * 方法的簽名可以視為方法的標識符,它用於區分不同的方法, * 即使它們有相同的名稱,只要它們的參數列表不同,就可以被視為不同的方法。 * * 在下方第一個例子中可以看到方法設計成需要傳入兩個參數, * 第一個是字串,而第二個是 int 數字。 * * 另外第一個例子傳入前的參數命名 str 跟 intVal * 與方法命名的 str 與 intVal 無關。 * 只要物件形態相同就可以,變數名稱可以依照當下的狀況去設計。 * * 在第二個例子就可以說明這個問題。 */ int intVal = 123; String str = "input something"; test(str, intVal); int myInt = 456; String string = "input anything"; test(string, myInt); /* * 接著演示傳入參數的小技巧 * 在最後一個變數的地方,使用「形態...名稱」的格式, * 就可以傳入零個至多個的參數,在方法裡面會成為陣列形態的參數 * 若沒有需要傳入參數,也可以直接忽略那個欄位 */ test31(str, str, str, str); //最後欄位可以傳入零至多個變數 test31(str); //也可以忽略最後的欄位 ExampleMethod2 testClass = new ExampleMethod2(); testClass.test32(str, str, str, str); //最後欄位可以傳入零至多個變數 testClass.test32(str); //也可以忽略最後的欄位 } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up