• 코드:
​x
 
1
class Car {
2
    private String modelName;
3
    private int modelYear;
4
    private String color;
5
    private int maxSpeed;
6
    private int currentSpeed;
7
    
8
    Car(String modelName, int modelYear, String color, int maxSpeed) {
9
        this.modelName = modelName;
10
        this.modelYear = modelYear;
11
        this.color = color;
12
        this.maxSpeed = maxSpeed;
13
        this.currentSpeed = 0;
14
    }
15
    
16
    public String getModel() {
17
        return this.modelYear + "년식 " + this.modelName + " " + this.color;
18
    }
19
}
20
​
21
public class prog {
22
    public static void main(String[] args) {
23
        Car myCar = new Car();                  // 기본 생성자의 호출
24
        // Car myCar = new Car("아반떼", 2016, "흰색", 200); // 생성자의 호출
25
        
26
        System.out.println(myCar.getModel());   // 생성자에 의해 초기화되었는지를 확인함.
27
    }
28
}
표준입력 & 실행옵션