본문 바로가기
개인공부

의미파악해보자 (추상클래스 / 인터페이스)

by 리승우 2022. 7. 31.
interface Flyable{
    void fly(int x, int y, int z);
}
class PPigeon implements Flyable {
    private int x, y, z;

    @Override
    public void fly(int x, int y, int z) {
        printLocation();
        System.out.println("이동합니다");
        this.x = x;
        this.y = y;
        this.z = z;
        printLocation();
    }


    public void printLocation() {
        System.out.println("현재위치 {" + x + "," + y + "," + z + ")");
    }
}
    public class jogun {
        public static void main(String[] args) {
        Flyable pigeon = new PPigeon();
        pigeon.fly(1,2,3);
        }
    }

������ġ {0,0,0)
�̵��մϴ�
������ġ {1,2,3)

 

abstract class Bird{
    private int x,y,z;

    void fly(int x, int y, int z) {
        printLocation();
        System.out.println("이동합니다");
        this.x = x;
        this.y = y;

        if(flyable(z)) {
            this.z = z;}
        else{
            System.out.println("그 높이로는 날 수 없습니다");
        }
        printLocation();
    }

    abstract boolean flyable(int z);

        public void printLocation(){
            System.out.println("현재위치 {" +x +","+y+","+z+")");
        }
    }

    class Pigeon extends Bird{
        @Override
        boolean flyable(int z) {
            return z<10000;
        }
    }

    class Peacock extends Bird{
        @Override
        boolean flyable(int z) {
            return false;
        }
    }

public class Main {
    public static void main(String[] args) {

        Bird pigeon = new Pigeon();
        Bird peacock = new Peacock();
        System.out.println("---비둘기---");
        pigeon.fly(1,1,3);
        System.out.println("---공작새---");
        peacock.fly(1,1,3);
        System.out.println("---비둘기---");
        pigeon.fly(3,3,30000);
    }
}

---��ѱ�---
������ġ {0,0,0)
�̵��մϴ�
������ġ {1,1,3)
---���ۻ�---
������ġ {0,0,0)
�̵��մϴ�
�� ���̷δ� �� �� �����ϴ�
������ġ {1,1,0)
---��ѱ�---
������ġ {1,1,3)
�̵��մϴ�
�� ���̷δ� �� �� �����ϴ�
������ġ {3,3,3)

'개인공부' 카테고리의 다른 글

NEW 연산자 /작성중  (0) 2022.07.31
JVM이란? (자바가상머신) /정리중  (0) 2022.07.31
디폴트 메서드와 static 메서드  (0) 2022.07.31
인터페이스의 장점2  (0) 2022.07.31
인터페이스의 장점  (0) 2022.07.28

댓글