즐거운프로그래밍

[자바] 값에 의한 호출과 객체에 의한 호출 : 전달받은 숫자를 1만큼 증가시키는 update메서드

수수께끼 고양이 2023. 11. 13. 17:57
728x90
반응형
class Updater {
    void update(Counter counter) {
        counter.count++;
    }
}

class Counter {
    int count = 0; // 객체 변수
}
public class Main {
    public static void main(String[] args) {
        Counter myCounter = new Counter();
        System.out.println("before update:"+myCounter.count);
        Updater myUpdater = new Updater();
        myUpdater.update(myCounter);
        System.out.println("after update:"+myCounter.count);
    }
}

 

 

 

 

728x90
반응형