Java Interface
- λ©μλ μκ·Έλμ²μ μμλ§ μ μ
- μ€κ³λ(Specification)μ κ°μ μν
- λ€μν λ°©λ²μΌλ‘ ꡬν(implementation) κ°λ₯
- 곡ν΅μ νμ
μν
example
public interface Car {
public int getSpeed();
public boolean accelerate();
}
public class Avante implements Car {
int speed;
@Override
public int getSpeed() {
return speed;
}
@Override
public boolean accelerate() {
speed += 10;
return false;
}
}
public class Matiz implements Car {
int speed;
@Override
public int getSpeed() {
return speed;
}
@Override
public boolean accelerate() {
speed += 20;
return false;
}
}
public class Drive {
public static void main(String[] args) {
Car avante = new Avante();
Car matiz = new Matiz();
drive(avante, "Avante");
drive(matiz, "Matiz");
}
private static void drive(Car car, String name) {
System.out.println(name);
for (int i = 0; i < 10; i++) {
car.accelerate();
System.out.println(i + ":" + car.getSpeed());
}
}
}