JAVA 객체 지향 핵심

(JAVA)메소드(method)란 뭘까? - 5

mynote6676 2025. 4. 15. 13:40

💡 학습 목표

메소드와 함수에 차이점을 이해 한다. 변수의 위치에 따라 지역변수와 멤버 변수로 부를 수 있다.

 

💡 객체의 속성은 멤버 변수로 객체의 기능은 메서드로 구현한다.

 

 

package ch07;

//학생 클래스를 설계하는 코드 측
public class student {

    int studentId;
    String studentName;
    String studentAddress;

    //행위

    public void study() {
        System.out.println("---------------------");
        System.out.println(studentName + "가 공부를 합니다");
    }

    public void breakTime() {
        System.out.println("---------------------");
        System.out.println(studentName + "가 휴식을 합니다.");
    }

    public void showInfo() {
        System.out.println("----------상태창-----------");
        System.out.println("학생 ID : " + studentId);
        System.out.println("학생 이름 : " + studentName);
        System.out.println("학생 주소 : " + studentAddress);
    }

    public void testing() {
        System.out.println("---------------------");
        System.out.println(studentName + "가 시험을 칩니다");

    }

    public void cleaning() {
        System.out.println("---------------------");
        System.out.println(studentName + "가 청소를 합니다");

    }

    //메서드란 기능을 구현하기 위해 클래스 내부에 구현되는 함수
    //메서드는 멤버 메서드라고 부릅니다.
    // !! 메서드는 함수와 다르게 멥버 변수를 활용해서 기능을 구현한다.

    // 함수와 메서드를 구분해서 부르자.
    //연습문제
    //1.시험을 친다라는 메서드를 만들어주세요

    //2. 청소를 한다 메서드를 만들어주세요

}

 

 

package ch07;

//코드를 실행하는측
public class studentMainTest {

    //코드의 진입점
    public static void main(String[] args) {

        student s1 = new student();
        s1.studentId = 1;
        s1.studentName = "티모";
        s1.studentAddress = "푸른 언덕";

        student s2 = new student();
        s2.studentId = 2;
        s2.studentName = "샤코";
        s2.studentAddress = "붉은 언덕";


        //객체에 동작을 시켜보자
        s1.showInfo();
        s2.showInfo();
        System.out.println("-------------------");
        s1.study();
        s2.study();
        System.out.println("----------------------");
        s1.breakTime();
        s2.breakTime();
        System.out.println("---------------------");
        //코드 (메서드 테스트)
        s1.testing();
        s2.cleaning();
        s2.cleaning();

    }
}