boolean type은 getter가 is로 시작한다.
나머지는 get으로 시작한다.
package education;
public class Car {
// 필드
private int speed;
private boolean stop;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if(speed < 0) {
this.speed = 0;
}else {
this.speed = speed;
}
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
this.speed = 0;
}
}
package education;
public class Car_Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.setSpeed(10);
System.out.println("현재 속도 : " + myCar.getSpeed());
myCar.setSpeed(-10);
System.out.println("현재 속도 : " + myCar.getSpeed());
myCar.setSpeed(60);
System.out.println("현재 속도 : " + myCar.getSpeed());
if(!myCar.isStop()) {
myCar.setStop(true);
}
System.out.println("현재 속도 : " + myCar.getSpeed());
}
}
댓글