본문 바로가기
JAVA SE/Code

CODE [20.07.24/Day_12] Java SE / Interface 인터페이스

by 파프리카_ 2020. 7. 24.
728x90
반응형

[ Interface 인터페이스 ]

[for문을 활용한 예제]

main class

/PlayerTestInterface2.java

public class TestInterface2 {
	public static void main(String[] args) {
		Player[] pa = { new Youtube(), new CDPlayer() };
		pa[0].play();
		pa[1].play();
		/*유튜브를 재생하다
		 * CD를 재생하다
		 */
		
		//인터페이스만 파악하면 사용가능하다
		for(int i=0; i<pa.length; i++) {
			pa[i].play();
		/*유튜브를 재생하다
		 * CD를 재생하다
		 */
		}
	}
}

 

Interface Class

/Player.java

interface Player {
	public void play();
}

 

Implement Classes

/Youtube.java

public class Youtube implements Player {
	@Override
	public void play() {
		System.out.println("유튜브를 재생하다");
	}
}

 

/CDPlayer.java

public class CDPlayer implements Player {
	@Override
	public void play() {
		System.out.println("CD를 재생하다");
	}
}

 


[Service class를 활용한 예제]

main class

/TestInterface3.java

package step3;

import step2.*;

public class TestInterface3 {
	public static void main(String[] args) {
		PlayerService service = new PlayerService();
		service.execute(new Youtube());
		service.execute(new CDPlayer());
		/* 동영상을 재생하다
		 * cd를 넣다
		 * cd를 재생하다
		 */
	}
}

 

Service Class

package step3;

import step2.*;

//Service class : 비즈니스 로직
public class PlayerService {
	public void execute(Player p) {
		if (p instanceof CDPlayer) {
			((CDPlayer) p).insertCd();
		}
		p.play();
	}
}

 

 

 

 

Interface Class

/Player.java

interface Player {
	public void play();
}

 

Implement Classes

/Youtube.java

package step2;

public class Youtube implements Player {
	@Override
	public void play() {
		System.out.println("동영상을 재생하다");
	}
}

 

/CDPlayer.java

package step2;

public class CDPlayer implements Player {
	@Override
	public void play() {
		System.out.println("CD를 재생하다");
	}

	public void insertCd() {
		System.out.println("CD를 넣다");
	}
}

 


[배열을 활용한 예제]

main class

/TestInterface5.java

public class TestInterface5 {
	public static void main(String[] args) {
		// 배열 length가 100
		FlyerService service = new FlyerService(100);
		service.addFlyer(new Bird());
		service.addFlyer(new Airplane());
		service.addFlyer(new SuperMan());
		
		service.executeFly();
		/* 새가 날다
		 * 비행기가 날다
		 * 슈퍼맨이 날다
		 */
	}
}

 

Service Class

/FlyerService.java

public class FlyerService {
	private Flyer[] array;
	private int index;
	
	public FlyerService(int length) {
		array = new Flyer[length];
	}
	
	public void addFlyer(Flyer f) {
		array[index++] = f;
	}
	
	public void executeFly() {
		for (int i=0; i < index; i++) {
			array[i].fly();
		}
	}
}

 

 

Interface Class

/Flyer.java

public interface Flyer {
	public void fly();
}

 

Implement Classes

/Bird.java

public class Bird extends Animal implements Flyer{
	
	//Flyer의 추상메서드 구현
	@Override
	public void fly() {
		System.out.println("새가 날다");
	}
}

 

/SuperMan.java

public class SuperMan implements Fighter, Flyer{
	//Fighter의 추상메서드 구현
	@Override
	public void fighter() {
		System.out.println("슈퍼맨이 악당과 싸우다");
	}
	
	//Flyer의 추상메서드 구현
	@Override
	public void fly() {
		System.out.println("슈퍼맨이 날다");
	}
}

 

/AirPlane.java

public class Airplane implements Flyer{
	@Override
	public void fly() {
		System.out.println("비행기가 날다");
	}
}

 

 

728x90
반응형