Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 메서드
- 에러페이지
- JSP
- oracle
- 기본
- paint
- 국제화
- OGNL
- Spring
- Menu
- 전화걸기
- struts2
- Java
- 생성자
- 배열
- HTML
- Graphic
- 클래스
- 안드로이드
- Eclips
- AWT
- JavaScript
- mybatis
- 어노테이션
- 메소드
- 예외처리
- 이클립스
- 오버로딩
- layout
- Android
Archives
- Today
- Total
note
추상 클래스 예제 본문
package com.abs;//추상 클래스 기본틀
//추상 클래스
public abstract class Unit {
protected String name;
protected int energy;
public int getEnergy(){
return energy;
}
//추상 메서드
abstract public void decEnergy(); // 에너지 소모
}
package com.abs;
public class Zerg extends Unit{
boolean fly;
public Zerg(String name, boolean fly){
this.name = name;
this.energy = 100;
this.fly = fly;
}
//unit의 추상메서드 구현
public void decEnergy(){
energy -= 6;
}
}
package com.abs;
public class Protoss extends Unit{
boolean fly;
public Protoss(String name, boolean fly){
this.name = name;
this.energy = 100;
this.fly = fly;
}
//unit의 추상메서드 구현
public void decEnergy(){
energy--;
}
}
package com.abs;
public class Terran extends Unit{
boolean fly;
public Terran(String name, boolean fly){
this.name = name;
this.energy = 100;
this.fly = fly;
}
//unit의 추상메서드 구현
public void decEnergy(){
energy -= 3;
}
}
package com.abs;
public class UnitTest {
public static void main(String[] args){
Zerg z1 = new Zerg("승리",false);
z1.decEnergy();
System.out.println("z1의 energe : "+z1.getEnergy());
Protoss p1 = new Protoss("행복",true);
p1.decEnergy();
System.out.println("p1의 energe : "+p1.getEnergy());
Terran t1 = new Terran("파도",false);
t1.decEnergy();
System.out.println("t1의 energe : "+t1.getEnergy());
}
}
z1의 energe : 94
p1의 energe : 99
t1의 energe : 97