멍발자의 개발

클래스와 메소드 본문

STUDY/Java

클래스와 메소드

개발하는 멍발자 2022. 3. 22. 14:50

클래스와 메소드

 

객체를 생성하는 것만으로도 메소드에 액세스 해 이용이 가능하게 된다.

package Chap10;

class Computer4 {
    String os;
    int memory;

    // 필드변수의 값을 표시하는 메소드
    public void show() {
        System.out.println("PC의 OS는 " + os + "입니다.");
        System.out.println("메모리 크기는 " + memory + "GB입니다.");
    }
}

public class InstanceMethod1 {

    public static void main(String[] args) {
        //Computer4 클래스에서 객체 생성
        Computer4 com = new Computer4();

        // 데이터를 저장하기 전에 메소드 호출 (1번째)
        System.out.println("* 1번째 메소드 호출");
        com.show();

        //객체의 필드변수에 각 데이터를 저장함
        com.os = "Windows 11";
        com.memory = 32;

        // 데이터를 저장 후 메소드 호출 2번째
        System.out.println("\n* 2번째 메소드 호출");
        com.show();
    }
}
// 결과
* 1번째 메소드 호출
PC의 OS는 null입니다.
메모리 크기는 0GB입니다.

* 2번째 메소드 호출
PC의 OS는 Windows 11입니다.
메모리 크기는 32GB입니다.

초기값을 설정 안 해 두면 "null"이 입력된다.

 

class Computer8 {

    String os;
    int memory;

    // 필드 변수의 값을 표시하는 메소드
    public void show() {
        System.out.println("OS는" + os + "입니다.");
        System.out.println("메모리는 " + memory + "GB입니다.");
    }
}

public class ObjectInitializationObjectVariable {
    public static void main(String[] args) {
        //Computer8형 객체변수 com1을 선언하고 객체 생성
        Computer8 com1 = new Computer8();

        com1.os = "Windows 11";
        com1.memory = 32;

        // Computer8형의 객체변수 com2를 선언과 동시에 다른 변수로 초기화
        Computer8 com2 = com1;
        System.out.println("com2에 com1을 할당합니다\n");

        // com1의 show 메소드에 액세스
        System.out.print("com1 PC ");
        com1.show();

        // com의 show 메소드에 액세스
        System.out.print("com2 PC");
        com2.show();
    }
}
//결과
com2에 com1을 할당합니다

com1 PC OS는Windows 11입니다.
메모리는 32GB입니다.
com2 PCOS는Windows 11입니다.
메모리는 32GB입니다.

this 키워드

 

클래스 내에 정의된 멤버 (속성, 메소드)의 머리에 붙이는 것이다.

"객체 자신의 필드 변수" "객체 자신의 메소드"

 

package Chap10;

class Computer9 {
    String os;
    int memory;

    //메시지와 pc 정보를 표시하는 메소드
    public void showComputer() {
        System.out.println("PC 정보를 표시합니다.");
        // 클래스 show 메소드 호출
        this.show(); //this 키워드 사용
    }
    // 필드 변수의값을 표시하는 메소드
    public void show() {
        System.out.println("OS는 " + this.os  + "입니다."); // this 키워드 사용
        System.out.println("메모리는 " + this.memory + "GB입니다."); // this 키워드 사용
    }
    //필드 변수에서 OS 값을 얻음
    public String getOs() {
        return this.os; // this 키워드 사용
    }

    //필드 변수에서 Memory 값을 가져옴
    public int getMemory() {
        return this.memory; //this 키워드 사용
    }

    // 필드 변수에 값을 설정하는 메소드
    public void setOsMemory(String os, int memory) {
        this.os = os; // this 키워드 사용
        this.memory = memory;// this 키워드 사용
        System.out.println("OS는 " + os + "이고. 메모리는 " + memory + "GB입니다.");
    }
}

public class ThisKeyWord {
    public static void main(String[] args){
        // Computer9클래스에서 객체 생성
        Computer9 com = new Computer9();

        //객체 필드변수에 각 데이터를 저장
        com.os = "Windows 11";
        com.memory = 16;

        // 메소드를 사용하여 필드 변수에 저장된 데이터 가져옴
        System.out.println("PC의 OS는" + com.getOs() + "입니다." );
        System.out.println("메모리는 " + com.getMemory() + "GB입니다.");

        //객체의 필드 변수에 각 데이터를 다시 저장
        com.setOsMemory("Windows 10", 8);

        //showComputer메소드를 호출하여 필드변수의 값 확인
        com.showComputer();
    }
}

//결과

PC의 OS는Windows 11입니다.
메모리는 16GB입니다.
OS는 Windows 10이고. 메모리는 8GB입니다.
PC 정보를 표시합니다.
OS는 Windows 10입니다.
메모리는 8GB입니다.

'STUDY > Java' 카테고리의 다른 글

생성자  (0) 2022.03.23
액세스와 캡슐화  (0) 2022.03.23
객체 지향 언어  (0) 2022.03.21
메소드  (0) 2022.03.18
반복 처리  (0) 2022.03.16
Comments