package com.trans; public class Transport { protected String name; protected int speed; protected int energy; //protected boolean swim; public String getName(){ return name; } public int getSpeed(){ return speed; } public int getEnergy(){ return energy; } public void consumGas(){ energy -= 10; } }
package com.trans; public class TransportMain { public static void main(String[] args){ Ship s = new Ship("배",25,50,true); s.consumGas(); Air a = new Air("비행기",50,100,true); a.consumGas(); System.out.println("=================================="); System.out.println("이름\t속도\t연료\t비고"); System.out.println("=================================="); System.out.print(s.getName()+"\t"); System.out.print(s.getSpeed()+"\t"); System.out.print(s.getEnergy()+"\t"); System.out.print((s.getSwim()?"항해" : "정박")+"\n"); System.out.print(a.getName()+"\t"); System.out.print(a.getSpeed()+"\t"); System.out.print(a.getEnergy()+"\t"); System.out.print((a.getFly()?"비행" : "착륙")+"\n"); } }
package com.trans; class Ship extends Transport{ private boolean swim=true; public void consumGas(){//메소드 오버라이딩으로 재정의 energy -= 25; } public Ship(String name,int speed,int energy,boolean swim){ this.name=name; this.speed=speed; this.energy=energy; } public boolean getSwim(){ return swim; } }
package com.trans; class Air extends Transport{ public void consumGas(){ energy -= 50; } private boolean fly=true; public Air(String name,int speed,int energy, boolean fly){ this.name=name; this.speed=speed; this.energy=energy; } public boolean getFly(){ return fly; } }
'Java > 연습문제' 카테고리의 다른 글
자판기 만들기 4(스위치문 ->if 문 브레이크로 while탈출) (0) | 2012.04.14 |
---|---|
자판기 만들기 3( 커피값받는통 거스름돈통 분리) (0) | 2012.04.14 |
자판기 만들기 2 (0) | 2012.04.14 |
자판기 만들기 (0) | 2012.04.13 |
성적 프로그램 입력받아서 출력하기 (0) | 2012.04.13 |