๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ๋””ํดํŠธ ๋ฉ”์„œ๋“œ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 20.
๋ฐ˜์‘ํ˜•

โ–ถ ๋””ํดํŠธ ๋ฉ”์„œ๋“œ

๋ง ๊ทธ๋Œ€๋กœ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ๋ฉ”์„œ๋“œ.

๋””ํดํŠธ ๋ฉ”์„œ๋“œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๊ตฌํ˜„ํ•˜์ง€๋งŒ, ์ดํ›„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ๊ทธ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•  ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์ด๋‹ค.

์„ ์–ธํ•  ๋•Œ default ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

package interfaceex;

public interface Calc {
	double PI = 3.14;
	int ERROR = -999999999;

	int add(int num1, int num2);

	int substract(int num1, int num2);

	int times(int num1, int num2);

	int divide(int num1, int num2);

	default void description() {
		System.out.println("์ •์ˆ˜ ๊ณ„์‚ฐ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค");
	}
    
}

 

package interfaceex;

public class CalculatorTest {
	public static void main(String[] args) {
		int num1 = 10;
		int num2 = 5;

		CompleteCalc calc = new CompleteCalc();
		System.out.println(calc.add(num1, num2));
		System.out.println(calc.substract(num1, num2));
		System.out.println(calc.times(num1, num2));
		System.out.println(calc.divide(num1, num2));
		calc.showInfo();
		calc.description();
	}
    
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

 

 

โ–ถ ๋””ํดํŠธ ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ํ•˜๊ธฐ

๋งŒ์•ฝ ์ด๋ฏธ ์ธํ„ฐํŽ˜์ด์Šค์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๋””ํดํŠธ ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ํด๋ž˜์Šค์—์„œ ์›ํ•˜๋Š” ๊ธฐ๋Šฅ๊ณผ ๋งž์ง€ ์•Š๋Š”๋‹ค๋ฉด, ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ๋””ํดํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

super.description( )์€ ์ธํ„ฐํŽ˜์ด์Šค์— ์„ ์–ธํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€