1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| package Lecture7;
/*
* 메소드 오버로딩(Overloading)
* : 같은 클래스 내에 동명의 메소드이면서 매개변수의 개수 또는 타입이 다른 것
*/
public class Method {
void call() {
System.out.println("call() 메소드 호출...");
}
void call(int i) {
System.out.println("call(int) 메소드 호출...");
}
/*
int call(int i) {
System.out.println("int call(int) 메소드 호출...");
}
*/
void call(double d) {
System.out.println("call(double) 메소드 호출...");
}
void call(String s) {
System.out.println("call(String) 메소드 호출...");
}
void call(char c) {
System.out.println("call(char) 메소드 호출...");
}
void call(String s, boolean b) {
System.out.println("call(String, boolean) 메소드 호출...");
}
void call(boolean b, String s) {
System.out.println("call(boolean, String) 메소드 호출...");
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| package Lecture7;
/*
* **********
* hello
* **********
* hi
* **********
* Good-bye
* **********
*/
public class MethodMain01 {
/**
* 임의의 문자를 반복하여 출력하는 기능
* @param c 출력할 문자
* @param cnt 출력 횟수
*/
static int printstar(char c, int cnt) { // printstar와 main method는 독립적으로 움직임
for(int i = 0; i < cnt; i++) {
System.out.print(c);
}
System.out.println();
return 10;
}
public static void main(String[] args) {
int data = printstar('*', 10); // printstar의 정의부로 이동 -> 작업수행 -> ;종료
// System.out.println("**********"); // 반복문은 연속적으로 문장이 나올 때 가능
System.out.println("hello");
printstar('|', 9); // char c = '|', int cnt = 9;
System.out.println("hi");
printstar('#', 8);
System.out.println("Good-bye");
printstar('-', 7);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| package Lecture7;
public class MethodMain02 {
public static void main(String[] args) {
Method m = new Method();
m.call();
m.call(10);
m.call(10.33);
m.call("Hello");
m.call('A');
m.call("hello", true); // 순서중요! m.call(true, "hello");는 다른거
m.call(true, "hello");
}
}
|