본문으로 건너뛰기

Java 객체지향 2: 상속과 오버라이딩


상속

class Unit {
private int damage;
private int hp;

public Unit(int dmg, int hp) {
damage = dmg;
this.hp = hp;
}
}

class ProtossUnit extends Unit {
static int shieldLv = 0;
private int shield;

public ProtossUnit(int dmg, int hp, int sh) {
super(dmg, hp);
shield = sh;
}
}

class ProtossGroundUnit extends ProtossUnit {
static int attackLv = 0;
static int defenseLv = 0;

public ProtossGroundUnit(int dmg, int hp, int sh) {
super(dmg, hp, sh);
}
}

public class Main {
public static void main(String[] args) {
ProtossGroundUnit probe = new ProtossGroundUnit(5, 20, 20);
}
}