|
| 1 | +package javastudy; |
| 2 | + |
| 3 | +//메서드 오버로딩(Method Overloading) |
| 4 | +//메서드 이름은 동일하지만 매개변수의 자료형이나 개수가 다르면 여러 개의 메서드를 만들어 놓고 사용할 수 있다 |
| 5 | + |
| 6 | + |
| 7 | +class Operator { |
| 8 | + |
| 9 | + //메서드 오버로딩 |
| 10 | + // 4 3 |
| 11 | + int multiply(int x, int y) { |
| 12 | + return x * y; //곱한 결과 값을 호출한 곳으로 돌려준다 |
| 13 | + } |
| 14 | + |
| 15 | + double multiply(double x, double y) { //매개변수의 자료형이 다르다 |
| 16 | + return x * y; //곱한 결과 값을 호출한 곳으로 돌려준다 |
| 17 | + } |
| 18 | + |
| 19 | + double multiply(int x, double y) { |
| 20 | + return x * y; //곱한 결과 값을 호출한 곳으로 돌려준다 |
| 21 | + } |
| 22 | + |
| 23 | + double multiply(double x, int y) { |
| 24 | + return x * y; //곱한 결과 값을 호출한 곳으로 돌려준다 |
| 25 | + } |
| 26 | + |
| 27 | + double multiply(double x) { //매개변수의 개수가 다르다 |
| 28 | + return x * x; //곱한 결과 값을 호출한 곳으로 돌려준다 |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +public class Chapter13 { |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + |
| 38 | + Operator op = new Operator(); |
| 39 | + |
| 40 | + //int multiply(int x, int y) 호출 |
| 41 | + op.multiply(4, 3); //메서드 호출도 하고 함수 내에서 반환한 값도 가지고 있다 |
| 42 | + |
| 43 | + int result1 = op.multiply(4, 3); |
| 44 | + System.out.println("result1 = " + result1); |
| 45 | + System.out.println("op.multiply(4, 3) = " + op.multiply(4, 3)); //호출, 결과 값을 가지고 있음 |
| 46 | + System.out.println("-------------------"); |
| 47 | + |
| 48 | + //double multiply(double x, double y) 호출 |
| 49 | + double result2 = op.multiply(4.5, 3.5); |
| 50 | + System.out.println("result2 = " + result2); |
| 51 | + System.out.println("op.multiply(4.5, 3.5) = " + op.multiply(4.5, 3.5)); |
| 52 | + System.out.println("-------------------"); |
| 53 | + |
| 54 | + System.out.println(1); |
| 55 | + System.out.println(5.5); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +} |
0 commit comments