JAVA/예제

[JAVA] ch07-09. 객체 지향 9

밍글링글링 2017. 8. 22.
728x90
 
public class Ex09 {
    public static void main(String[] args){
        Time time = new Time(23,35,30);
        System.out.println(time);
        //time.hour=1;
        time.setHour(time.getHour()+1);
        System.out.println(time);
    }
}

class Time{     
    private int hour;
    private int minute;
    private int second;
    
    Time(int hour, int minute, int second){
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }
    
    public int getHour(){
        if(hour>=23) hour=hour-24;
        return hour;
    }
    
    public void setHour(int hour){
        //if(hour<0||hour>23) return; //품질낮은코드
        if(0<=hour && hour<48)
            this.hour = hour;
    }
    
    public int getMinute(){
        return minute;
    }
    
    public void setMinute(int minute){
        if(minute<0||minute>59) return;
        this.minute = minute;
    }
    public int getSecond(){
        return second;
    }
    
    public void setSecond(int second){
        if(second<0||second>59) return;
        this.second = second;
    }
    
    public String toString(){
        return hour+":"+minute+":"+second;
    }
}

//메소드이름
//데이터 쓰는 것은 set == setter
//데이터 읽는 것은 get == getter
//set get 뒤에는 변수
//ex) public int getHour(){}, public void setHour(){}

728x90

'JAVA > 예제' 카테고리의 다른 글

[JAVA] ch07-11. 객체 지향 11  (0) 2017.08.22
[JAVA] ch07-10. 싱글톤 기법 (singleton)  (0) 2017.08.22
[JAVA] ch07-08. 객체 지향 8  (0) 2017.08.22
[JAVA] ch07-06. 객체 지향 6  (0) 2017.08.22
[JAVA] ch07-05. 객체 지향 5  (0) 2017.08.22

댓글