클래스와 객체·19 / 22
메서드로 동작 넣기
필드뿐 아니라 메서드도 클래스 안에 정의할 수 있습니다. 메서드 안에서 자기 필드를
쓰려면 this.필드로 접근합니다.
class Counter {
int count;
void increase(int n) {
this.count = this.count + n;
}
}
Counter c = new Counter();
c.increase(3);
System.out.println(c.count);c.increase(3)처럼 객체 뒤에 .으로 메서드를 호출합니다.
할 일
balance에 amount를 더하세요 (this.balance 사용)
막혔나요?
코드 에디터