๊ด€๋ฆฌ ๋ฉ”๋‰ด

โœ๐Ÿป๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ

[ JAVA ] ์ถ”์ƒํด๋ž˜์Šค ๋ณธ๋ฌธ

Language/Java

[ JAVA ] ์ถ”์ƒํด๋ž˜์Šค

์ฉ์‹œํ‚ด 2020. 12. 20. 22:38
728x90

์ฃผ๋กœ ์ƒ์†์˜ ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ์‚ฌ์šฉ๋จ

 

์ถ”์ƒ ํด๋ž˜์Šค ์„ ์–ธ ํ˜•์‹

abstract ๋ฐ˜ํ™˜ํƒ€์ž… ๋ฉ”์†Œ๋“œ์ด๋ฆ„();

๊ตฌํ˜„๋œ ๋ฉ”์„œ๋“œ๋Š” ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์˜ ๋ฉ”์„œ๋“œ , ํ•˜์œ„ ํด๋ž˜์Šค์— ๋”ฐ๋ผ ์žฌ์ •์˜๊ฐ€ ๊ฐ€๋Šฅ

์ถ”์ƒ ํด๋ž˜์Šค๋Š” new (์ธ์Šคํ„ด์Šคํ™”)๊ฐ€ ๋ถˆ๊ฐ€ํ•˜๋‹ค → ๋ฉ”์„œ๋“œ์˜ ๋ฐ”๋””๊ฐ€ ์—†์œผ๋ฏ€๋กœ

์˜ˆ์‹œ) ์ž์ฃผ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“ˆ์˜ ๊ณตํ†ต๋˜๋Š” ๋ถ€๋ถ„์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค๊ณ  , ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ์ชฝ์—์„œ ์ž์‹ ์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉ, ์ƒ์‚ฐ์„ฑ์ด ํ–ฅ์ƒ

 

๋ชจ๋“  ๋ฉ”์„œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์—ˆ๋‹ค๊ณ  ํ•ด๋„ ํด๋ž˜์Šค์— abstract ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ถ”์ƒ ํด๋ž˜์Šค

→ jdk ๋‚ด๋ถ€์—๋„ ์กด์žฌ, ์˜ค๋กœ์ง€ ์ƒ์†์„ ์œ„ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค

 

๋ฐ˜์ฏค ์ฑ„์›Œ์ง„ ์•„์ด์ฝ˜์ด ์ถ”์ƒํด๋ž˜์Šค๋ฅผ ๊ฐ€๋ฅดํ‚ด

package abstractex;

public abstract class Computer {

    public abstract void display();
    public abstract void typing();

    public void turnOn(){
        System.out.println("์ „์›์„ ์ผญ๋‹ˆ๋‹ค.");
    }

    public void turnOff(){
        System.out.println("์ „์›์„ ๋•๋‹ˆ๋‹ค.");
    }
}

 

๊ฐ€์žฅ ์ƒ์œ„์— ์„ ์–ธ๋œ ์ถ”์ƒ ํด๋ž˜์Šค Computer. 

์ƒ์†๋ฐ›์„ Computerํด๋ž˜์Šค์— ์„ ์–ธ๋œ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋Š” ์ƒ์†๋ฐ›์€ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ๋Š” ๋ฌด์กฐ๊ฑด ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋งŒ์•ฝ ์ƒ์†๋ฐ›์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๋ฅผ ๋งŒ๋‚œ๋‹ค.

Computer ์ƒ์œ„ ํด๋ž˜์Šค์— ์„ ์–ธ๋œ ์ถ”์ƒํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์ง€์•Š์€ ๊ฒฝ์šฐ

Computer์˜ turnOn(), turnOff() ๋ฉ”์„œ๋“œ๋Š” ์ถ”์ƒ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ์ƒ์†๋ฐ›์•„์•ผ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋‹ค.

package abstractex;

public class DeskTop extends Computer {

    @Override
    public void display() {
        System.out.println("DeskTop display");
    }

    @Override
    public void typing() {
        System.out.println("DeskTop typing");
    }
    
}

 

๋‘ ๋ฒˆ์งธ๋กœ Computer ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ NoteBookํด๋ž˜์Šค 

์ƒ์†๋ฐ›์€ ์ถ”์ƒ ํด๋ž˜์Šค์— ์ •์˜๋œ ๋ฉ”์„œ๋“œ ์ค‘ ์ผ๋ถ€๋งŒ ๊ตฌํ˜„ํ–ˆ๋‹ค๋ฉด ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋Š” ์•„์ง abstract ํด๋ž˜์Šค

NoteBook ํด๋ž˜์Šค๋Š” Computerํด๋ž˜์Šค์—์„œ ์„ ์–ธํ•œ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์ถ”์ƒ ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.

๋งŒ์•ฝ NoteBook ํด๋ž˜์Šค๋ฅผ ์ผ๋ฐ˜ ํด๋ž˜์Šค๋กœ ์ •์˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Computerํด๋ž˜์Šค์—์„œ ์ •์˜ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋‘ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.

package abstractex;

public abstract class NoteBook extends Computer{

    @Override
    public void typing() {
        System.out.println("NoteBook typing");
    }
}

 

๋‚˜๋จธ์ง€ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์€ display๋ฉ”์„œ๋“œ๋Š” NoteBookํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํ•˜์œ„ ํด๋ž˜์Šค์ธ MyNoteBook ํด๋ž˜์Šค์— ์„ ์–ธํ•˜์˜€๋‹ค.

package abstractex;

public class MyNoteBook extends NoteBook{
    @Override
    public void display() {
        System.out.println("MyNoteBook display");
    }
}

 

์‹คํ–‰ ํ…Œ์ŠคํŠธ

package abstractex;

public class ComputerTest {

    public static void main(String[] args) {
        // Computer computer = new Computer(); ์ถ”์ƒํด๋ž˜์Šค์ด๋ฏ€๋กœ ์ธ์Šคํ„ด์Šคํ™” ๋ถˆ๊ฐ€
        Computer computer = new DeskTop();//์ƒ์œ„ ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ํ•˜์œ„ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šคํ™” ํ˜•๋ณ€ํ™” ๊ฐ€๋Šฅ
        computer.display();
        computer.turnOff();

        //Computer myNote = new MyNoteBook();//์ƒ์œ„ ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ์„ ์–ธ์ด ๊ฐ€๋Šฅ 
        NoteBook myNote = new MyNoteBook();//ํ•ญ์ƒ ์ƒ์œ„ํด๋ž˜์Šค๋ฅผ ๋‚ดํฌํ•˜๊ณ ์žˆ์Œ

    }

}

 

 

 

728x90
๋ฐ˜์‘ํ˜•