본문 바로가기

개인 공부/하루 공부

3/17 공부

*6) 메소드

 

- 메소드

    // 메소드 정의
    public static void sayHello() {
        System.out.println("안녕하세요? 메소드 입니다.");
    }



    public static void main(String[] args) {
        // 메소드 호출
        System.out.println("메소드 호출 전");
        sayHello(); //  == System.out.println("안녕하세요? 메소드 입니다."); 같은거
        System.out.println("메소드 호출 후");
    }
}
        메소드 호출 전
        안녕하세요? 메소드 입니다.
        메소드 호출 후

 

 

- 전달값

    public static void chicken (int nene) { // chicken > 메소드 이름 , nene > 정수
        int nenechincken = nene * nene;
        System.out.println(nene + " 의 2승은 " + nenechincken );
    }


    public static void main (String[] args) {
        // 전달값, Parameter
        // 2 -> 2 * 2 = 4
        // 3 -> 3 * 3 = 9

        // 인수
        chicken(2);
        chicken(3);
    }
}

        2 의 2승은 4
        3 의 2승은 9

 

- 메소드 오버로딩 (전)

       // 메소드 오버로딩
   public static int getGubneChicken (int gubne) { // 교촌치킨 - int
        int result = gubne * gubne;
        return result;
        // return number * number;
    }



    public static int getCheogasjibChicken(String cheogasjib) { // 처갓집치킨 - String
        int gubne = Integer.parseInt(cheogasjib);
        return gubne * gubne;

    }


    public static int getFranchiseChick(int gubne, int exponent) { // 프렌차이즈 - (int)교촌, (int) exponent
        int result = 1;
        for (int i = 0; i < exponent; i++) {
            result *= gubne;
        }
        return result;
    }

    
    public static void main(String[] args) { // main에 출력문
        // 메소드 오버로딩
        System.out.println(getGubneChicken(3)); // 3 * 3 = 9
        System.out.println(getCheogasjibChicken("4")); // 4 * 4 = 16
        System.out.println(getFranchiseChick(3,3)); // 3 * 3 * 3 = 27
    }
}

        9
        16
        27

 

- 메소드 오버로딩 (후) : 전달값 타입, 자료형,갯수 가 다르면 같은 이름의 메소드를 중복해서 선언가능.

    // 메소드 오버로딩 : 전달값 타입, 자료형,갯수 가 다르면 같은 이름의 메소드를 중복해서 선언가능(메소드 오버로딩 가능)

    public static int getFranchiseChick (int gubne) {
        int result = gubne * gubne;
        return result;
        // return number * number;
    }

    
//    // 반환형이 다른경우 (double)중복불가.
//    public static double getFranchiseChick (int gubne) {
//        return 0.0;
//    }

    
    // 전달값의 타입이 다름
    public static int getFranchiseChick(String cheogasjib) {
        int gubne = Integer.parseInt(cheogasjib);
        return gubne * gubne;

    }

    // 갯수가 다른경우
    public static int getFranchiseChick(int gubne, int exponent) {
        int result = 1;
        for (int i = 0; i < exponent; i++) {
            result *= gubne;
        }
        return result;
    }


    public static void main(String[] args) { // main에 출력문
        // 메소드 오버로딩
        // 같은 이름의 메소드를 여러번 선언
        // 1. 전달값의 타입이 타드러나
        // 2. 던
        System.out.println(getFranchiseChick(3)); // 3 * 3 = 9
        System.out.println(getFranchiseChick("4")); // 4 * 4 = 16
        System.out.println(getFranchiseChick(3,3)); // 3 * 3 * 3 = 27
    }
}

'개인 공부 > 하루 공부' 카테고리의 다른 글

3/21 공부  (0) 2023.03.21
3/19  (0) 2023.03.19
3/18 공부  (0) 2023.03.19
3/16 공부  (0) 2023.03.17
3/14) 공부  (0) 2023.03.14