본문 바로가기

백엔드개발자 준비하기

[백엔드개발자 준비하기] 인터페이스

인터페이스 내에서는 abstract를 붙이지 않아도 모두 추상이다
-인터페이스 내부에 있는 객체는 구현하는 객체가 아님
-일반메소드 작성 불가능 (default를 붙여줘야 사용가능)
-일반 변수(멤버변수)를 가지지 못함
-상수는 가질 수 있음
-인터페이스 내에서 변수를 선언하면 final을 붙이지 않더라도 자동으로 상수로 선언됨

implements
인터페이스를 갖다쓰겠다

자바는 단일상속이라 여러개의 클래스를 상속받을 수 없다
인터페이스는 여러개를 갖다 쓸 수(상속 비슷함)있음

 

[interface Calculator]

public interface Calculator {
	
	public int ERROR = -9999999;
	
	public double puls(double x, double y);
	public double minus(double x, double y);
	
	public default double multiplication(double x, double y) {
		return x * y;
	}
	
	public double division(double x, double y);
}

[Equipment]

public abstract class Equipment {

	public abstract void powerOn();
	public abstract void powerOff();
}

[Computer]

public class Computer implements Calculator{

	@Override
	public double puls(double x, double y) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double minus(double x, double y) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double division(double x, double y) {
		// TODO Auto-generated method stub
		return 0;
	}

}

[GeneralCalculator]

public class GeneralCalculator extends Equipment implements Calculator {

	@Override
	public void powerOn() {
		System.out.println("일반 계산기 전원을 켭니다.");
	}

	@Override
	public void powerOff() {
		System.out.println("일반 계산기 전원을 끕니다.");
	}

	@Override
	public double puls(double x, double y) {
		System.out.println("일반계산기에서 더하기 실행");
		return x + y;
	}

	@Override
	public double minus(double x, double y) {
		System.out.println("일반계산기에서 빼기 실행");
		return x - y;
	}

	@Override
	public double division(double x, double y) {
		System.out.println("일반계산기에서 나누기 실행");
		if (x == 0 || y == 0) {
			return ERROR;
		}
		return x / y;
	}

}

[SmartPhone]

public class SmartPhone extends Equipment implements Calculator{

	@Override
	public void powerOn() {
		System.out.println("스마트폰 전원을 켭니다.");
	}

	@Override
	public void powerOff() {
		System.out.println("스마트폰 전원을 끕니다.");
	}

	@Override
	public double puls(double x, double y) {
		System.out.println("스마트폰에서 더하기 실행");
		return x + y;
	}

	@Override
	public double minus(double x, double y) {
		System.out.println("스마트폰에서 빼기 실행");
		return x - y;
	}

	@Override
	public double division(double x, double y) {
		System.out.println("스마트폰에서 나누기 실행");
		if (x == 0 || y == 0) {
			return ERROR;
		}
		return x / y;
	}
}

[Main]

	public static void main(String[] args) {
		Equipment[] equipments = new Equipment[2];
		
		equipments[0] = new GeneralCalculator();
		equipments[1] = new SmartPhone();
		
		Calculator calculator = new Computer();
		
		System.out.println("모든 장비 전원 켜기");
		for (Equipment eq : equipments) {
			eq.powerOn();
		}
		System.out.println();
		
		System.out.println("모든 장비에서 10과 20을 더하기");
		for (Equipment eq : equipments) {
			Calculator c = (Calculator) eq;
			double result = c.puls(10, 20);
			
			System.out.println("결과 : " + result);
		}
		System.out.println();
		
		System.out.println("모든 장비에서 10과 2을 나누기");
		for (Equipment eq : equipments) {
			Calculator c = (Calculator) eq;
			double result = c.division(10, 2);
			
			System.out.println("결과 : " + result);
		}
		System.out.println();
		
		System.out.println("모든 장비 전원 끄기");
		for (Equipment eq : equipments) {
			eq.powerOff();
		}
		System.out.println();
	}