자바 메소드와 클래스

이상혁·2024년 1월 11일
0

Method

함수와 같다.
자바의 클래스에서 행동, 기능을 담당한다.

package exercise.chapter_24;

public class MethodExamples {
    public static void main(String[] args) {
        char ch = 'A';
        int decode = toUnicode(ch);
        System.out.println(decode);

        int myInt = 10;
        int myInt2 = 5;
        int result = sumAndMultiplyFour(myInt, myInt2);
    }

    static int toUnicode (char ch) {
        return (int) ch;
    }

    static int sumAndMultiplyFour(int a, int b) {
        return (a + b) * 4;
    }
}

class

class는 객체를 만드는 설계도이다.
그 안에 속성을 나타내는 멤버 변수들과 기능을 나타내는 메소드로 이루어져 있다.
그리고 이 class를 이용해서 객체를 생성하고 안에 변수들과 메소드들을 사용할 수 있다.

package exercise.Chapter_25;

public class Fish {
    String sex;
    boolean havingPoison;
    String startSpawningDate;
    String endSpawningDate;
    String leavingSea;

    void eat(String food) {
        System.out.printf("나, 물고기는 %s를 먹고 있습니다", food);
    }

    void swim(int meter) {
        System.out.println("나는 헤엄칩니다 미터: " + meter);
    }

}
profile
개발 공부 하기 위해 만든 블로그

0개의 댓글