본문 바로가기

IT/자바

인터페이스 2-1(tv)

MyMain_TV.java

Channel.java

RemoteCon.java

Tv.java

Volume.java



실선이면 : extend (상속)
쩜쩜으로 들어가면 implements (인터페이스)
package mymain;

import inter.RemoteCon;
import inter.Tv;

public class MyMain_TV {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
        //사용설명서        =  제품설계서     
		//interface            class 
		RemoteCon remotecon = new Tv();
		
		System.out.println("TV를 키세요");
		while(true) {
			int key=System.in.read();
			if(key=='o')
				remotecon.onOff();
			else if(key=='+')
				remotecon.volumeUp();
			else if(key=='-')
				remotecon.volumeDown();
			
			
		}
		
/*		remotecon.showTv();
		
		//켜기
		remotecon.onOff();
		
		//채널변경
		remotecon.channelUp();
		remotecon.setChannel(30);
		
		//볼륨변경
		remotecon.volumeUp();
*/
	}

}
package inter;

public class Tv implements RemoteCon{

	boolean bOnOff;//꺼짐/켜짐상태
	int volume;    //볼륨상태
	int channel;   //채널상태
	
	public Tv() {
		// TODO Auto-generated constructor stub
		volume  = 10;
		channel = 9; 
	}
	
	
	@Override
	public void volumeUp() {
		// TODO Auto-generated method stub
		volume++;
		if(volume > Volume.MAX_VOLUME)
			volume = Volume.MAX_VOLUME;
		
		showTv();
	}

	@Override
	public void volumeDown() {
		// TODO Auto-generated method stub
		volume--;
		if(volume < Volume.MIN_VOLUME)
			volume = Volume.MIN_VOLUME;
		
		showTv();
	}

	@Override
	public void volumeZero() {
		// TODO Auto-generated method stub
		volume = Volume.MIN_VOLUME;
		
		showTv();
	}

	@Override
	public void channelUp() {
		// TODO Auto-generated method stub
		channel++;
		if(channel > Channel.MAX_CHANNEL)
			channel = Channel.MIN_CHANNEL;
		
		showTv();
		
	}

	@Override
	public void channelDown() {
		// TODO Auto-generated method stub
		channel--;
		if(channel< Channel.MIN_CHANNEL)
			channel = Channel.MAX_CHANNEL;
		
		showTv();
		
	}

	@Override
	public void setChannel(int channel) {
		// TODO Auto-generated method stub
		//채널유효범위 체크
		
		this.channel = channel;
				
		showTv();
	}

	@Override
	public void onOff() {
		// TODO Auto-generated method stub
		//Toggle방식 : ture <-> false
		bOnOff = !bOnOff;
		
		showTv();
		
	}

	@Override
	public void showTv() {
		// TODO Auto-generated method stub
		if(bOnOff==false) {
			System.out.println("---현재 TV 꺼짐---");
			return;
		}
		
		//껴진상태
		System.out.println("-------[ TV 켜짐]----------");
		System.out.printf("--채널 : %d\n", channel);
		System.out.printf("--볼륨 : %d\n", volume);
		System.out.println("---------------------------");		
	}

}
package inter;

//인터페이스간에는 다중상속이 가능
//인터페이스를 상속받을 떄는 extends 사용
public interface RemoteCon extends Volume,Channel{

	void onOff();
	void showTv();
}
package inter;

public interface Channel {
	/* public static final */
	int MIN_CHANNEL=1;
	int MAX_CHANNEL=100;
	
	/* abstract Method */
	void channelUp();
	void channelDown();
	void setChannel(int channel);
}
package inter;

public interface Volume {
	/* public static final */
	int MIN_VOLUME = 0; 
	int MAX_VOLUME = 30;
	
	/* abstract Method */
	void volumeUp();
	void volumeDown();
	void volumeZero();
}


'IT > 자바' 카테고리의 다른 글

템플릿 만들기  (0) 2018.05.08
BaseWin, MyWin  (0) 2018.05.08
인터페이스 2 tv 컨트롤  (0) 2018.05.08
인터페이스1  (0) 2018.05.08
추상클래스와 인터페이스 차이  (0) 2018.05.08