2007.11.25

일상/글로 보기 | 2007/11/25 22:27 | 와이지

1.
가끔씩 재미로 해보는 나의 상태를 OOP로 나타내기..
Source code:

public class Person {

	private String name;

	public Person(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

}

public class YG extends Person {

	private int tired = 0;

	private int worked = 0;

	public YG() {
		super("YG~");
	}

	public void doWork() {
		System.out.println(getName() + " is working...");
		if (isTired()) {
			doRest();
		}
		tired++;
		worked++;
		doWork();
	}

	public void doRest() {
		System.out.println(getName() + " is taking a break...");
		int rest = (worked / 2);
		if (rest == 0) {
			throw new RuntimeException("쉬어도 쉬어도 피로해...");
		}
		tired -= rest;
		worked = 0;
	}

	private boolean isTired() {
		return (tired > 10);
	}

	public static void main(String[] args) {
		YG me = new YG();
		me.doWork();
	}

}
Output:
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is taking a break...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is working...
YG~ is taking a break...
YG~ is working...
YG~ is working...
YG~ is taking a break...
YG~ is working...
YG~ is taking a break...
Exception in thread "main" java.lang.RuntimeException: 쉬어도 쉬어도 피로해...
	at YG.doRest(YG.java:25)
	at YG.doWork(YG.java:14)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.doWork(YG.java:18)
	at YG.main(YG.java:37)
  1. kj 2007/11/26 00:30 답글수정삭제

    너무나 심심하여.. ㅡㅡ;;(루비로.. ㅡㅡ;;)

    class Person
    attr_reader :name
    def initialize(name)
    @name = name
    end
    end
    class YG < Person
    def initialize(tired=0, worked=0)
    super('YG')
    @tired = tired
    @worked = worked
    end
    def istired
    @tired > 10
    end
    def dorest
    p "#{@name} is taking a break..."
    rest = @worked / 2
    if rest == 0
    raise "rest and rest.. but i feel so tired.."
    end
    @tired -= rest
    @worked = 0
    end
    def dowork
    p "#{@name} is working..."
    if(istired)
    dorest
    end
    @tired = @tired + 1
    @worked = @worked+ 1
    dowork
    end
    end

    YG.new.dowork

  2. kj 2007/11/27 22:28 답글수정삭제

    syntax highlighting과 indent가 안들어가서 ^^;;
    나중에는 웹에 올릴때는 같이 올려야 겠네요~ ㅎ 그럼 전 이만 =3

  3. 2007/11/28 12:29 답글수정삭제

    개발자들이란.... -_-

트랙백 주소 :: http://blog.thebum.net/86/trackback/
옵션
댓글 달기
이전 1 ... 26 27 28 29 30 31 32 33 34 ... 75 다음