| English | 의미 |
|---|---|
| accessor/əkˈsesə/ | 액세서 |
| mutator/mjuːˈteɪtə/ | 뮤테이터 |
A rejected update leaves the previous state intact
- A score starts at 20. setScore(s) assigns only when s >= 0; the call setScore(-5) leaves score at 20.
- The guard prevents an invalid update. A later getScore() returns the existing state rather than the rejected argument.
Writing a method
- A method has a header and a body. Header:
public void addScore(int s)— access, return type, name, parameters. - Body: the statements in
{ }that do the work. The method can read and change the object's instance variables.
Return values
- A method's return type says what it hands back; use
return value;to send it.public int getScore() { return score; }returns the current score. - A
voidmethod returns nothing — it just does its job (e.g. printing). The returned value must be assignment-compatible with the declared return type; for example, anintvalue can be widened and returned by adoublemethod.
public double result() { return 3; }라는 메서드는 3이 int 리터럴임에도 불구하고 컴파일될 수 있습니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
Java는 int 결과를 double로 확장합니다. 반환값은 동일한 타입으로 명시되어 있을 필요는 없으나 호환성이 있어야 합니다.
Accessors and mutators
- An accessor (getter) 访问方法 returns information without changing the object:
getScore(). A mutator (setter) 修改方法 changes the object's state:addScore(s)orsetName(n). - Getters are usually
int/String-returning; setters are usuallyvoid. Together they form the object's public interface over its private data.
액세서(accessor)는 값을 반환함
점수 20에서 시작하여 setScore(-5)는 업데이트를 거절하며, getScore는 20을 반환합니다.
객체를 변경하지 않고 정보만 반환하는 메서드는 ...입니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
액세서는 읽고 반환하며, 뮤테이터는 상태를 변경합니다.
void 메서드는 ...
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
void = 반환 값 없음; 단순히 동작을 수행함.
A non-void method may reach its closing brace and complete normally without returning a value.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
A path completing normally must return a compatible value. A path that throws an exception is a different case.
어떤 메서드가 상태 변경 전에 입력을 검증할 수 있는가?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
세터는 할당 전에 확인(예: 음수 거부)할 수 있습니다.
void 메서드 내에서 반환 값을 작성하면 컴파일은 정상입니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
void 메서드는 값을 반환할 수 없습니다 —那样则无法编译。
Methods can validate
- Because outside code goes through methods, a method can check before it acts.
public void setScore(int s) { if (s >= 0) score = s; }rejects a negative score. - Private access prevents unrelated code from directly assigning the field. Constructors and every class method that writes it must still preserve the intended rule; the guarded setter cannot repair an invalid initial score by itself.
A non-void method must return a compatible value on every path that completes normally. A path may instead throw an exception. If an int method reaches its end without returning, compilation fails. A void method returns nothing, so return; (or no return) is correct there; writing return value; in a void method won't compile.
For the stated guarded setter, what does getScore() return after setScore(-5)?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
The condition fails, so no assignment occurs and the old score remains 20.
점수 20에서 시작하여 replaceScore(30)는 기존 점수를 저장한 뒤 30을 할당하고, 마지막에 기존 점수를 반환합니다. 이 호출이 반환하는 값은 무엇입니까?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
반환된 기존 점수는 20이며, 새로 저장된 점수는 30입니다. 값을 반환한다고 해서 변형이 방지되는 것은 아닙니다.
An accessor and a mutator:
- Accessor:
public int getScore() { return score; }— reads, returnsint. - Mutator:
public void addScore(int s) { score += s; }— changes state, returns nothing. student.addScore(10); int now = student.getScore();
Carry the reasoning to a new case
- Consider
public int replaceScore(int s) { int old = score; score = s; return old; }. It is a mutator even though it returns a value: starting at 20, replaceScore(30) returns 20 and leaves the score at 30. - Judge behavior rather than names or return types. A method called getScore that increments the score is not a pure accessor, and a void method that merely prints a message need not mutate the object.
A method has a header (access, return type, name, parameters) and a body. A non-void method must return an assignment-compatible value on every normally completing path; a void method returns nothing. Accessors (getters) return state without changing it; mutators (setters) change state and can validate the new value.