자바

interface RemoteControl

package java8;

public interface RemoteControl {

	public static final int NUM=100; // 상수만 가능
	
	//추상 메소드만 허용됨
	public void turnOn();
	public void turnOff();
	
	//JDK 1.8 부터 default, static method 허용
	public default void start(){}
	public static void print(){}
	
}

 

class Television

package java8;

public class Television implements RemoteControl{

	boolean onOff=false;
	@Override
	public void turnOn() {
		onOff=true;
		System.out.println("Tv 전원을 켭니다.");
	}

	@Override
	public void turnOff() {
		onOff=false;
		System.out.println("Tv전원을 끕니다");
		
	}

	public static void main(String[] args) {
		RemoteControl tv =new Television();
		tv.turnOn();
		tv.turnOff();
	}
	
	
}

 

class Refreigerator

package java8;

public class Refreigerator implements RemoteControl {

	boolean onOff=false;
	@Override
	public void turnOn() {
		onOff=true;
		System.out.println("냉장고 전원을 켭니다.");
	}

	@Override
	public void turnOff() {
		onOff=false;
		System.out.println("냉장고 전원을 끕니다");
		
	}

	public static void main(String[] args) {
		Refreigerator re =new Refreigerator();
		re.turnOn();
		re.turnOff();
	}
	

}

 

 

class CallbackTest

package java8;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;


class MyCalass implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("beep");
	}
	
}

public class CallbackTest {

	
	
	public static void main(String[] args) {
		ActionListener listener=new MyCalass();
	
		//javax.swing.Timer
		Timer timer=new Timer(1000, listener);
		timer.start();//타이머를 시작함
		
		
		for(int i=0; i<1000; i++){
			try{
				Thread.sleep(1000); 
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	
}

 

=>

beep
beep
beep
beep
beep
beep

 

 

 

class ArrayTest

package java8;

public class ArrayTest {

	public static void main(String[] args) {
		int[] nums={10, 20,30, 40,50};
		try{
			
			for(int i=0; i<=5; i++){
				System.out.println(nums[i]);
			}
			
		}catch(Exception e){
			System.out.println("배열 인덱스 값 오류");
			
			//디버깅을 위한 코드
			e.printStackTrace();
		}
		System.out.println("프로그램 진행");
	}
	
}

 

=>

10
20
30
40
50
배열 인덱스 값 오류
프로그램 진행
 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

놀고 있는 사람들 속에서 혼자 부지런히 일하고, 자고 있는 사람들 속에서 혼자 자지 않는 사람이 되라. -법구경

댓글 ( 4)

댓글 남기기

작성