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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | class Method { public static void main(String[] args) { //메소드 오버로딩, Method Overloading // - 같은 이름의 메소드를 여러가 선언 // - 메소드 인자리스트를 다르게 구성하는 전제하에 // - 개발자를 위한., //요구사항] 두개의 데이터를 더하는 메소드 // 1. int + int // 2. double + double // 3. Stinrg + String // 4. 모든 기본자료형 대상으로 구현 sum(10, 20); //sum((int)1.5, (int)2.3); sum(1.5, 2.3); sum("홍길동", "아무게"); /* 메소드 오버로딩 조건 O 1. 인자의 갯수 2. 인자의 타입 메소드 오버로딩 조건 X 1. 인자의 이름 2. 반환값 타입 코드 작성중.. 호출 : test(); -> 1번이 대답 test(10); -> ? test("홍길동"); -> 4번 test(10, 20); -> 5번 int result = test(10); 1. public static void test() {} //o 2. public static void test(int n) {} //o 3. public static void test(int m) {} //x(2번과 충돌) 4. public static void test(String s) {} //o 5. public static void test(int n, int m) {} //o 6. public static int test(int n) {} //x(2번과 충돌, 호출 시 구분이 안감) */ } public static void sum(int a, int b) //자바는 이름을 'sum'이 아닌 'sum(int a, int b)'으로 //인식하기 때문에 넘버링 안해도 에러x //인자리스트가 달라졌으므로, { System.out.printf("%d + %d = %d\n", a, b, a + b); } public static void sum(double a, double b) { System.out.printf("%.1f + %.1f = %.1f\n", a, b, a + b); } public static void sum(String a, String b) { System.out.printf("%s + %s = %s\n", a, b, a + b); } } | cs |
'WEB > JAVA' 카테고리의 다른 글
메소드6 (예제) (0) | 2015.05.22 |
---|---|
메소드5 (재귀 메소드) (0) | 2015.05.22 |
메소드3 (지역변수) (0) | 2015.05.22 |
메소드2 (구성요소) (0) | 2015.05.22 |
메소드1 (0) | 2015.05.22 |