void 메서드 다루기
클래스는 메서드를 통해 동작하며, 메서드는 항상 특정 데이터 타입을 반환할 필요가 없어요. 이번에는 Car 클래스 안에 자동차 시동을 켜는 메서드를 구현해 보세요.
이 연습은 강의의 일부입니다
Java로 배우는 객체 지향 프로그래밍 입문
연습 안내
- 매개변수가 없는
void메서드turnEngineOn을Car클래스 안에 만드세요. turnEngineOn메서드 안에서"engine is on"메시지를 출력하세요.- 이미 생성되어 있는
myCar객체 인스턴스에서turnEngineOn을 호출하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class Main {
static class Car {
String color;
String model;
int year;
Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
// Create the turnEngineOn method
____ ____() {
// Print out "engine is on"
System.out.println("____");
}
}
public static void main(String[] args) {
Car myCar = new Car("red", "camry", 2022);
// Call the turnEngineOn method on the myCar object instance
____.____();
}
}