[Java] μΈν°νμ΄μ€λ₯Ό μ΄μ©ν λ€νμ±κ³Ό νμ©
μΈν°νμ΄μ€μ λ€μ€ μμ
μμ μΈν°νμ΄μ€λ λ€μ€μμμ΄ κ°λ₯νλ€κ³ νμ΅λλ€.
public interface Attackable {
void attack(int power);
}
public interface Movable {
void move(int m);
}
public interface Fightable extends Movable, Attackable {
void attackAndMove(int power, int m);
}
- ν΄λμ€ μμκ³Ό λ§μ°¬κ°μ§λ‘ μΈν°νμ΄μ€λ λΆλͺ¨ μΈν°νμ΄μ€μ μ μλ λ©€λ²λ₯Ό λͺ¨λ μμλ°μ΅λλ€.
- Fightableμ λ³ΈμΈμ λ©€λ² attackAndMove()μ λΆλͺ¨ λ©€λ²μ attack(), move()λ₯Ό λ©€λ²λ‘ κ°κ² λ¨.
public class Fighter implements Fightable {
@Override
public void attack(int power) {
}
@Override
public void attackAndMove(int power, int m) {
}
@Override
public void move(int m) {
}
}
- μ΄ λ λΆλͺ¨μ μ κ·Ό μ μ΄μλ public abstractκ° μλ΅λ κ²μ΄κΈ° λλ¬Έμ (μΈν°νμ΄μ€μ νΉμ§) ν΄λμ€μμ ꡬνν λ λΆλͺ¨μ μ μ΄μλ₯Ό λ°λμ publicμΌλ‘ μ€μ ν΄μΌ ν©λλ€.
μΈν°νμ΄μ€μμλ λ€μ€μμμΌλ‘ μΈν λ¬Έμ κ° μμκΉ?
ν΄λμ€μμ λ€μ€μμμ΄ μλλ μ΄μ λ λ λΆλͺ¨ ν΄λμ€κ° λμΌν μκ·Έλμ²μ λ©μλλ₯Ό κ°κ³ μμμ΄ μ΄ λ ν΄λμ€λ₯Ό μμλ°λ κ²½μ°, μμ μ μ₯μμλ μμλ°μ λ μ΄λ€ ν΄λμ€μ λ©μλλ₯Ό μμλ°μμΌ νλ μ§ μ μ μμ΅λλ€.
νμ§λ§ μΈν°νμ΄μ€μμλ μ΄ λ¬Έμ λ₯Ό νΌν΄κ° μ μμ΅λλ€. why ? λ©μλκ° μ μλμ§ μμκΈ° λλ¬Έ
- μ΄μ°¨νΌ λΆλͺ¨ λ©μλκ° κ΅¬νλμ§ μμκΈ° λλ¬Έμ, μμ ν΄λμ€μμλ λ μ€ μ΄λ κ²μ μμ λ°μλ ꡬνν΄μ£Όμ΄μΌ νλ―λ‘, μ΄λ€ λΆλͺ¨λ‘λΆν° μμλ°μλμ§μ λν΄μλ μ νμκ° μμ΅λλ€.
public interface Mom {
void giveMoney(int money);
}
public interface Father {
void giveMoney(int money);
}
public class Child implements Father, Mom {
@Override
public void giveMoney(int money) {
}
}
μΈν°νμ΄μ€λ₯Ό μ΄μ©ν λ€νμ±
λ€νμ±μ λν΄ λ€λ£° λ μμ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λΆλͺ¨νμ μ μ°Έμ‘°λ³μλ‘ μ°Έμ‘°νλ κ²μ΄ κ°λ₯νλ€κ³ νμμ΅λλ€.
public class Tv {
protected String model;
}
public class SmartTv extends Tv {
protected String smartFunction;
private void printSmartFunction(){
System.out.println(this.smartFuntion);
}
}
public class SmartTvTest {
public static void main(String[] args){
Tv t = new SmartTv();
}
}
μΈν°νμ΄μ€ μμ μ΄λ₯Ό ꡬνν ν΄λμ€μ μ‘°μμ΄λΌ ν μ μμΌλ―λ‘ ν΄λΉ μΈν°νμ΄μ€ νμ μ μ°Έμ‘°λ³μλ‘ μ΄λ₯Ό ꡬνν ν΄λμ€(μμ)μ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°ν μ μμΌλ©°, μΈν°νμ΄μ€ νμ μΌλ‘ νλ³ν λν κ°λ₯ν©λλ€.
public interface Fightable {
void attack(int power);
}
public class Fighter implements Fightable{
@Override
public void attack(int power) {
System.out.println(power + "μ λ°λ―Έμ§λ‘ 곡격");
}
}
public class FighterTest {
public static void main(String[] args) {
Fightable f = new Fighter();
f.attack(50);
}
}
μ€ν κ²°κ³Ό
50μ λ°λ―Έμ§λ‘ 곡격
- λ°λΌμ λΏλ§ μλλΌ Up-casting(λΆλͺ¨ → μμ)λ κ°λ₯ν©λλ€.
public class FighterTest {
public static void main(String[] args) {
Fightable f = (Fightable) new Fighter();
f.attack(50);
}
}
- μΈν°νμ΄μ€λ λ€μκ³Ό κ°μ΄ λ©μλμ λ§€κ°λ³μμ νμ μΌλ‘ μ¬μ©λ μ μμ΅λλ€.
public interface Fightable {
void attack(int power, Fightable f);
String getName();
}
public class Fighter implements Fightable{
private String name;
public Fighter(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public void attack(int power, Fightable f) {
System.out.println("Fighter attack " + f.getName() + " with " + power);
}
}
public class Boxer implements Fightable {
private String name;
public Boxer(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public void attack(int power, Fightable f) {
System.out.println("Boxer attack " + f + " with " + power);
}
}
public class FighterTest {
public static void main(String[] args) {
Fightable f = new Fighter("fighter1");
f.attack(50, new Boxer("boxer1"));
}
}
μ€ν κ²°κ³Ό
Fighter attack boxer1 with 50
리ν΄νμ μ΄ μΈν°νμ΄μ€μΌ λ
λ©μλμ 리ν΄νμ μ΄ μΈν°νμ΄μ€ βΆοΈ λ©μλκ° ν΄λΉ μΈν°νμ΄μ€λ₯Ό ꡬνν ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό 리ν΄ν©λλ€.
μμ μ½λ
public interface Parseable {
void parse(String fileName);
}
public class HTMLParser implements Parseable {
@Override
public void parse(String fileName) {
System.out.println(fileName + " HTML Parsing");
}
}
public class XMLParser implements Parseable {
@Override
public void parse(String fileName) {
System.out.println(fileName + " XML Parsing");
}
}
public class ParserManager {
public static Parseable getParser(String type) {
if (type.equals("XML")) {
return new XMLParser();
} else{
return new HTMLParser();
}
}
}
public class ParserTest {
public static void main(String[] args) {
Parseable pm = ParserManager.getParser("XML");
pm.parse("doc.xml");
}
}
μ μ½λμμ μΆν μλ‘μ΄ μ’ λ₯μ XML Parser ν΄λμ€κ° λμλ μλ μ½λμ κ°μ΄ λ¦¬ν΄ λΆλΆλ§ λ³κ²½νλ©΄ λ©λλ€.
if (type.equals("XML")) {
// return new XMLParser();
return new NewXMLParser();
}
μΈν°νμ΄μ€μ μ₯μ
- κ°λ° μκ° λ¨μΆ
- μΈν°νμ΄μ€κ° μμ±λλ©΄ μ μΈλΆλ§ μλ©΄ μ΄λ₯Ό μ¬μ©ν΄μ νλ‘κ·Έλ¨μ μμ±νλ κ²μ΄ κ°λ₯
- νμ€ν κ°λ₯
- νλ‘μ νΈμ μ¬μ©λλ κΈ°λ³Έ νμ μΈν°νμ΄μ€λ‘ μμ±ν λ€μ, κ°λ°μλ€μκ² μΈν°νμ΄μ€λ₯Ό ꡬννμ¬ νλ‘κ·Έλ¨μ μμ±νλλ‘ ν¨μΌλ‘μ¨ λ³΄λ€ μΌκ΄λκ³ μ ννλ νλ‘κ·Έλ¨μ κ°λ°μ΄ κ°λ₯
- μλ‘ κ΄κ²μλ ν΄λμ€λ€μκ² κ΄κ³λ₯Ό λ§Ίμ΄μ€ μ μμ
- μμκ΄κ³μ μμ§λ μκ³ , κ°μ λΆλͺ¨ ν΄λμ€λ₯Ό κ°μ§κ³ μμ§ μμ μλ‘ μ무 κ΄κ³λ μλ ν΄λμ€λ€μκ² νλμ μΈν°νμ΄μ€λ₯Ό 곡ν΅μ μΌλ‘ ꡬννλλ‘ νμ¬ κ΄κ³λ₯Ό λ§Ίμ΄μ€ μ μμ΅λλ€.
μΈν°νμ΄μ€ - μΆμν΄λμ€ - ꡬν ν΄λμ€ λμμΈ ν¨ν΄
μΈν°νμ΄μ€μ ꡬν ν΄λμ€ μ¬μ΄μ μΆμ ν΄λμ€λ₯Ό νλ λκ³ μ΄ μΆμ ν΄λμ€μ ꡬν ν΄λμ€μ 곡ν΅, μ€λ³΅ λΆλΆλ€μ λͺ¨μλλ ν¨ν΄μ λλ€.
μμ
public interface Animal {
void walk();
void run();
void sleep();
void eat();
}
public class Lion implements Animal {
private String sex; // μ€λ³΅
private String color; // μ€λ³΅
public String getSex() { // μ€λ³΅
return sex;
}
public String getColor() { // μ€λ³΅
return color;
}
@Override
public void walk() {
System.out.println("Lion WALK");
}
@Override
public void run() {
System.out.println("Lion RUN");
}
@Override
public void sleep() {
System.out.println("Lion SLEEP");
}
@Override
public void eat() {
System.out.println("Lion EAT");
}
}
public class Tiger implements Animal{
private String sex; // μ€λ³΅
private String color; // μ€λ³΅
public String getSex() { // μ€λ³΅
return sex;
}
public String getColor() { // μ€λ³΅
return color;
}
@Override
public void walk() {
System.out.println("Tiger WALK");
}
@Override
public void run() {
System.out.println("Tiger RUN");
}
@Override
public void sleep() {
System.out.println("TIGER SLEEP");
}
@Override
public void eat() {
System.out.println("TIGER EAT");
}
}
Tigerμ Lion ν΄λμ€λ₯Ό 보면 sex, color, getSex(), getColor() μ½λμ μ€λ³΅μ΄ λ°μνλλ° μ΄ λ μ€λ³΅ λ©€λ²λ€μ μΆμν΄λμ€λ₯Ό λ§λ€μ΄ μ€λ³΅μ μ κ±°ν μ μμ΅λλ€.
package aboutInterface.test3;
public abstract class Mammal implements Animal{
private String sex; // μ€λ³΅
private String color; // μ€λ³΅
public String getSex() { // μ€λ³΅
return sex;
}
public String getColor() { // μ€λ³΅
return color;
}
}
μ΄ λμμΈ ν¨ν΄μ μ€λ³΅ μ κ±°μ ν¨κ³Όμ μ΄μ§λ§, ν΄λμ€ μμμ κΈ°λ°μΌλ‘ νκ³ μκΈ° λλ¬Έμ λ€λ₯Έ ν΄λμ€λ₯Ό μμλ°μμΌ νλ κ²½μ°μλ ν΄λμ€λ λ€μ€μμμ΄ μλκΈ° λλ¬Έμ μ΄ ν¨ν΄μ μ¬μ©ν μ μμ΅λλ€.
<μ°Έκ³ μλ£>
β μΈν°νμ΄μ€ vs μΆμν΄λμ€ μ©λ μ°¨μ΄μ - μλ²½ μ΄ν΄
μΈν°νμ΄μ€ vs μΆμν΄λμ€ λΉκ΅ μ΄ κΈμ μ°Ύμ보λ λ μλΆλ€μ μλ§λ μ΄λ―Έ μΈν°νμ΄μ€μ μΆμν΄λμ€ κ°λ μ νμ΅ν λ€μ μ΄ λμ λνμ¬ μ°¨μ΄μ λͺ¨νΈν¨ λλ¬Έμ λ°©λ¬Έ νκ² μ§λ§, κ·Έλλ λ€μνλ²
inpa.tistory.com
[Java] μΈν°νμ΄μ€(interface) μ΄ μ 리
μλ‘ Spring 곡λΆνλ μ€ interfaceλΆλΆμ λν΄ μ΄ν΄κ° λΆμ‘±ν κ² κ°μ μμΈνκ² νλ² μ λ¦¬ν΄ λ³΄λ €κ³ ν©λλ€. λͺ©μ°¨ 1. μΈν°νμ΄μ€λ? 2. μΈν°νμ΄μ€μ μμ± λ°©λ² 3. μΈν°νμ΄μ€μ μμ 4. μΈν°νμ΄μ€μ
dkswnkk.tistory.com