๋ฐ์ํ
1. [๋ถ๋ชจ] ๐จrealization > Printable์ธํฐํ์ด์ค
package realization;
public interface Printable {
//์ถ์๋ฉ์๋(Abstract Method) : ์์ํด๋์ค์์ ๋ฐ๋์ ์ค๋ฒ๋ผ์ด๋ ์ ์ธ
void print();
//JDK8 ์ด์์์๋ ์ธํฐํ์ด์ค์ ๋ช
๋ น์ ์์ฑํ ์ ์๋ ๊ธฐ๋ฅ์ ๊ธฐ๋ณธ๋ฉ์๋ ์ ์ธ ๊ฐ๋ฅ
//๊ธฐ๋ณธ๋ฉ์๋(Default Method) : ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ์์ํด๋์ค์ ์ค๋ฒ๋ผ์ด์ค ์ ์ธํ์ง ์์๋ ๋๋ ๋ฉ์๋ - ์ค๋ฒ๋ผ์ด๋ ์ ์ธํ์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๋ฉ์๋ ํธ์ถ
//ํ์)default ๋ฐํ๋ช
๋ฉ์๋๋ช
(์๋ฃํ ๋งค๊ฐ๋ณ์๋ช
,...) { ๋ช
๋ น;... }
default void scan() {
System.out.println("[์๋ฌ]์ค์บ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์์ต๋๋ค.");
}
}
2. [์์] ๐จrealization > PrintSingleํด๋์ค
package realization;
public class PrintSingle implements Printable {
@Override
public void print() {System.out.println("[ํ๋ฆฐํธ]๋ฌธ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.");}
}
3. [์์] ๐จrealization > PrintMultiํด๋์ค
package realization;
public class PrintMulti implements Printable {
@Override
public void print() {System.out.println("[๋ณตํฉ๊ธฐ]๋ฌธ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.");}
@Override
public void scan() {System.out.println("[๋ณตํฉ๊ธฐ]๋ฌธ์๋ฅผ ์ค์บํฉ๋๋ค.");}
}
4. [์์] ๐จrealization > PrintableApp ์คํํ๋ก๊ทธ๋จ
package realization;
public class PrintableApp {
public static void main(String[] args) {
//1.
//๊ธฐ๋ณธ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ฐธ์กฐ๋ณ์๋ฅผ ์ธํฐํ์ด์ค๋ก ์ ์ธ
Printable printOne=new PrintSingle();
printOne.print(); //[ํ๋ฆฐํธ]๋ฌธ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
//๋ถ๋ชจ ์ธํฐํ์ด์ค์ ๊ธฐ๋ณธ๋ฉ์๋ ํธ์ถ
printOne.scan(); //[์๋ฌ]์ค์บ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์์ต๋๋ค.
//2.
Printable printTwo=new PrintMulti();
printTwo.print(); //[๋ณตํฉ๊ธฐ]๋ฌธ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
printTwo.scan(); //[๋ณตํฉ๊ธฐ]๋ฌธ์๋ฅผ ์ค์บํฉ๋๋ค.
}
}
๋ฐ์ํ
'java > realization' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[realization] 7. ์ค์ฒฉํด๋์ค(Nested Class) (0) | 2024.06.12 |
---|---|
[realization] 6. ์์ํ๋(Constant)์ ์ด๊ฑฐํ(Enum) (1) | 2024.06.11 |
[realization] 4. ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถฐ ์ ์ง๋ณด์์ ํจ์จ์ฑ ๋์ด๋ ๋ฐฉ๋ฒ (1) | 2024.06.10 |
[realization] 3. ๐ขBoat๐Car๐ฅRealApp (0) | 2024.06.10 |
[realization] 2. ๐ฆWolfHumanApp (0) | 2024.06.09 |