Lesson 84: Classes and objects
이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
One class, many objects
- A class is a plan for objects: it lists their attributes (data) and methods (what they can do).
- An object is one thing made from the class.
Light red(12);makes an object and runs the constructor, which sets its attributes. - Private attributes can only be used by the class's own methods. A getter is a public method that returns one of them.
| OOP word | In this lesson |
|---|---|
| class | Light |
| objects | red, green |
| attributes | pin, timesOn (private) |
| methods | begin, on, off, getTimesOn |
- Write the methods below the class, like
void Light::on() { ... }. (If you write a method's body inside the class, this simulator's compiler step does not accept it.)
English
In the exam
- 9618/32/M/J/23 Q4: match OOP terms to descriptions. Mark scheme: encapsulation is the process of putting data and methods together as a single unit; getters are methods used to return the value of a property; setters are methods used to update it.
난이도 4/5 · 280코인 + 오늘 첫 완료 시 작은 보너스 추가
그것은 class Light describes any LED: its attributes are pin 및 timesOn, and its methods are what it can do. red 및 green are two objects of the class. Write the methods on 및 off; on also adds 1 to timesOn. Then the two LEDs take turns.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
The program wants to print how often the red light was on, but timesOn is private: code outside the class cannot read it, so red.timesOn does not compile. Add a public getter method, int getTimesOn(), that returns timesOn, and use red.getTimesOn() in the print.
출력을 보려면 '실행'을 클릭하세요.