편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.
최신판 | 당신의 편집 | ||
1번째 줄: | 1번째 줄: | ||
==개요== | ==개요== | ||
; | ;메소드란 호출할때 실행되는 코드 블록, 함수라고도 부른다 | ||
;메소드는 Class 내부에서 선언되어진다 | |||
* 기본 예제 | * 기본 예제 | ||
<syntaxhighlight | <syntaxhighlight lang='java' run> | ||
public class Main { | public class Main { | ||
static void myMethod() { | |||
System.out.println("메소드를 호출하면 출력됩니다"); | |||
} | |||
public static void main(String[] args) { | |||
myMethod(); | |||
} | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* 파라미터를 포함하여 | *파라미터를 포함하여 호출할수 있다 | ||
<syntaxhighlight | <syntaxhighlight lang='java' run> | ||
public class Main { | public class Main { | ||
static void myMethod(String fname) { | |||
System.out.println(fname + " 파라미터를 전달하여 호출할수 있습니다"); | |||
} | |||
public static void main(String[] args) { | |||
myMethod("사과"); | |||
myMethod("바나나"); | |||
myMethod("키위"); | |||
} | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
30번째 줄: | 33번째 줄: | ||
*메소드 오버라이딩 가능 | *메소드 오버라이딩 가능 | ||
*동일 메소드 이름으로 서로다른 변수를 받고 리턴할수 있다 | *동일 메소드 이름으로 서로다른 변수를 받고 리턴할수 있다 | ||
<syntaxhighlight | <syntaxhighlight lang='java' run> | ||
public class Main { | public class Main { | ||
static int plusMethod(int x, int y) { | |||
return x + y; | |||
} | } | ||
static double plusMethod(double x, double y) { | |||
return x + y; | |||
} | |||
public static void main(String[] args) { | |||
int myNum1 = plusMethod(8, 5); | |||
double myNum2 = plusMethod(4.3, 6.26); | |||
System.out.println("int: " + myNum1); | |||
System.out.println("double: " + myNum2); | |||
} | |||
} | |||
</syntaxhighlight> | |||