Frog is cry
getter_setter 본문
package getter_setter;
public class Car {
private int speed;
private boolean stop;
// Method
public int getSpeed() { // 리턴타입 int
return speed;
}
public void setSpeed(int speed) { // 리턴타입 void
if(speed < 0) {
this.speed = 0;
return;
}else {
this.speed = speed;
}
}
public boolean isStop() { // boolean -> is로 시작
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
// if(stop) {
speed = 0;
// }
}
}
package getter_setter;
public class Car_Ex {
public static void main(String[] args) {
Car myCar = new Car();
myCar.setSpeed(60); // setter : 값 저장
System.out.println("현재속도 : "+ myCar.getSpeed()); // getter : 값 불러오기
if(myCar.isStop()) { //움직이고 있다면
myCar.setStop(true); // 중지해라
}
System.out.println("현재 속도 : " + myCar.getSpeed());
}
}
Comments