KH 정보교육원/JAVA

#18일차

bameh 2019. 10. 23. 22:05

P.229

public class CarClass
{
    int speed;
    int getSpeed(){
        return speed;
    }
    void keyTurnOn() {
        System.out.println("키를 돌립니다.");
    }
    void breakOn() {
        System.out.println("브레이크를 작동합니다.");
    }
    void run() {
        for(int i=10; i<=50; i+=10){
             this.speed = i;
             System.out.println("달립니다.(시속: " + speed + "km/h)");
        }
    }
    void keyTurnOff() {
        System.out.println("시동이 꺼집니다.");
    }
}

class CarClass2
{
    public static void main(String[] args)
    {
        CarClass myCarClass = new CarClass();
        myCarClass.keyTurnOn();
        myCarClass.run();
        int speed = myCarClass.getSpeed();
        System.out.println("(현재 속도: "+ speed + "km/h)");
        myCarClass.keyTurnOff();
    }
}

 

P.235

<This>

public class This
{
    String model;
    int speed;

    This(String model){
        this.model = model;
    }

    void setSpeed(int speed){
        this.speed = speed;
    }

    void run() {
        for(int i=10; i<=50; i+=10){
            this.setSpeed(i);
            System.out.println(this.model + "가 달립니다.(시속: " + this.speed + "km/h)");
        }
    }
}

 

<ThisEx>

public class ThisEx
{
    public static void main(String[] args){
        This myThis = new This("포르쉐");
        This yourThis = new This("벤츠");
        This myThis2 = new This("K-5");

        myThis.run();
        yourThis.run();
        myThis2.run();
    }
}

'KH 정보교육원 > JAVA' 카테고리의 다른 글

#18일차  (0) 2019.10.23
#14?  (0) 2019.10.17
#14일차, 과제  (0) 2019.10.16
#14일차  (0) 2019.10.16
#13일차  (0) 2019.10.16