[toc]
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
# Method:
## Defining
```
modifiers return-type method-name(parameter-list) { // method body
}
```
---
### 參數
parameter name不能重複
```java
public static void f(int i, double i) { // invalid
}
```
如果無parameter, 寫空的`()`
```java
public static void printHello() {
System.out.println("Hello World!");
}
```
回傳型態void代表無回傳值
## Method Signature
Method Name & Parameter Types, 用來唯一標示一個方法
一個class內不能有同樣Method-Signature的方法。
Example:
```java
public static int sum(int i, int j) {
return i + j;
}
```
Method Signature:
```
sum(int, int)
```
---
可知, return-type / modifier不同, 不代表Method Signature不同
```java
public static int sum(int i, int j) {
return i + j;
}
```
```java
private long sum(int i, int j) {
return i + j;
}
```
Method Signature:
```
sum(int, int)
```
## Overloading
如果有多個方法使用同個名子, 但是Method Signature不同, 即為Overloading。
```java
public class Printer {
public static void print(int i) {
// ...
}
public static void print(double f) {
// ...
}
public static void print(String s) {
// ...
}
}
```
註: 請妥善使用, 否則程式碼可讀性有機會變差。
## VarArgs
如果最後一個Parameter是Array
或是只有一個Array Parameter
可以用Type... 代替
```java=
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
double d = 1.0;
f(d, arr);
// f(d, 1, 2, 3, 4, 5); // not allowed
g(d, arr);
g(d, 1, 2, 3, 4, 5);
}
static void f(double d, int[] arr) {
//...
}
static void g(double d, int... arr) {
//...
}
}
```
這在哪裡看過?
有的, 像是printf
```java==
public static void printf(String format, Object... args) {
// ...
}
```
## 傳入Primitive Data Type Arguments
基本類型, 傳入參數時是被passed by *value*
也就是一個value的copy
在Method內的任何改動都是在該Method內
Method return後, 改動不存在。
```java
public class PassPrimitiveByValue {
public static void main(String[] args) {
int x = 3;
// invoke passMethod() with
// x as argument
passMethod(x);
// print x to see if its
// value has changed
System.out.println(x); // 3
}
// change parameter in passMethod()
public static void passMethod(int p) {
p = 10;
}
}
```
## 傳入Reference Data Type Arguments
參照類型, 也是passed by *value*
但是因為傳入的是參照的copy
在Method內對該參照參照的物件的內容的改變, 在Method Return後影響仍在。
```java
class IntWrapper {
int i;
}
public class PassReferenceByValue {
public static void main(String[] args) {
IntWrapper x = new IntWrapper();
intWrapper.id = 3;
// invoke passMethod() with
// x as argument
passMethod(x);
// print x to see if its
// value has changed
System.out.println(x); // 10
}
// change parameter in passMethod()
public static void passMethod(IntWrapper p) {
p.i = 10;
}
}
```
# return
結束Method
(return不一定寫在最後一行,可以寫多個return)
return-type非void的方法:
-> 執行最後必須return value;(不代表在最後一行)
return-type為void的方法:
-> 在最後一行沒必要寫return;
```java=
public class Main {
public static void main(String[] args) {
f();
g("Hello");
int a = 10;
int b = 20;
int g = h(a,b);
System.out.println(g); //20
h(30,40); //沒有用到回傳的int
}
//無回傳的h方法,無參數
static void f() {
System.out.println("Hey!");
}
//無回傳值的g方法,參數有s
static void g(String s) {
if (s == null)
return; //如果s == null, 結束Method
System.out.println(s+"?");
return; //非必要
}
//回傳int的f方法,參數有i,j
static int h(int i,int j) {
if (i > j) {
return i;
}
return j;
}
}
```