public class Tire {
    public void go() {
        System.out.println("바퀴가 굴러갑니다.");
    }
}

 

public class SnowTire extends Tire {
    // 재정의
    public void go() {
        System.out.println("눈에서 안전하게 바퀴가 굴러갑니다.");
    }

    public void chain() {
        System.out.println("타이어에 체인을 추가로 장착할 수 있습니다.");
    }
}

 

public class KumhoSnowTire extends SnowTire {
    public void go() {
        System.out.println("금호의 신기술이 적용된 바퀴가 눈을 밀어내며 굴러갑니다.");
    }

    public void push() {
        System.out.println("눈을 바퀴 옆으로 밀어낸다.");
    }
}

 

public class InstanceOfApp {
    public static void main(String[] args) {
    /*
        객체 instanceof 클래스
        Tire t = new Tire();
        t instanceof Tire <-- t가 바라보는 객체가 Tire 종류인가요? true
        t instanceof SnowTire <--t가 바라보는 객체가 SnowTire류 인가요? true
        t instanceof KumhoSnowTire <-- t가 바라보는 객체가 KumhoSnowTire류인가요?
    */

    SnowTire t1 = new SnowTire();
    System.out.println("t1은 Tire류인가요?" + (t1 instanceof Tire));
    System.out.println("t1은 SnowTire류인가요?" + (t1 instanceof SnowTire));
    System.out.println("t1은 KumhoSnowTire류인가요?" + (t1 instanceof KumhoSnowTire));
    }
}

 

public class HankookSnowTire extends SnowTire {
    public void go() {
        System.out.println("한국의 신기술이 적용된 타이어가 눈을 녹이면서 갑니다.");
    }

    public void melt() {
        System.out.println("바퀴로 문을 녹이다.");    
    }
}​

 

public class TireTester {

    public void testTire(SnowTire tire) {
        if (tire instanceof HankookSnowTire) {
            HankookSnowTire t = (HankookSnowTire) tire;
            t.melt();
        } else if (tire instanceof KumhoSnowTire) {
            ((KumhoSnowTire) tire).push();
        }
    }
}

 

public class TireTesterApp {
    public static void main(String[] args) {
        
        TireTester tester = new TireTester();

        Tire t1 = new Tire();
        //tester.testTire(t1);    // 에러. testTire(SnowTire tire)메소드는 SnowTire류 객체만 전달받을 수 있다.

        KumhoSnowTire t2 = new KumhoSnowTire();
        tester.testTire(t2);

        HankookSnowTire t3 = new HankookSnowTire();
        tester.testTire(t3);
    }
}

 

public class TireApp {
    public static void main(String[] args) {
    
    KumhoSnowTire t1 = new KumhoSnowTire();
    SnowTire t2 = new KumhoSnowTire();
    Tire t3 = new KumhoSnowTire();

    t1.go();
    t2.go();
    t3.go();

    }
}

 

public class TireApp2 {
    public static void main(String[] args) {
        
        Tire t1 = new KumhoSnowTire();
        Tire t2 = new HankookSnowTire();
        Tire t3 = new SnowTire();
        Tire t4 = new Tire();

        t1.go();
        t2.go();
        t3.go();
        t4.go();
    }
}

 

public class TireApp3 {
    public static void main(String[] args) {
        // 에러 : 금호스노우타이어 객체를 한국스노우타이어 타입으로 형변환할 수 없다.
        // HankookSnowTire t1 = new KumhoSnowTire();

        // 에러 : 타이어 객체를 스노우타이어 타입으로 형 변환 할 수 없다.
        // 부모 타입 객체를 자식 타입의 참조 변수에 담을 수 없다.
        // SnowTire t1 = new Tire();
    }
}

 

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

    Tire t1 = new KumhoSnowTire();
    t1.go();
    t1.chain();    // 에러
    t1.push();    // 에러

    SnowTire t2 = (SnowTire) t1;
    t2.go();
    t2.chain();
    t2.push();

    }
}

 

public class Pen {
    public void draw() {
        System.out.println("그리다.");
    }
}

 

public class Redpen extends Pen {
    // 재정의
    public void draw() {
        System.out.println("빨갛게 그리다.");
    }

}

 

public class BluePen extends Pen {
    public void draw() {
        System.out.println("파랗게 그리다.");
    }
}

 

public class Painter {
    
    /* 
        매개변수의 다형성
            fillColor 메소드와 drawShape 메소드는 Pen을 상속받는 모든 Pen들을 전달받을 수 있다.
            메소드 내에서 p.draw()를 실행하면 전달받은 XXXPen 객체에 재정의된 draw()가 실행된다.
            * Pen의 종류와 상관없이 draw를 실행하면 실제로 전달받은 객체의 재정의된 draw()가 실행돼서
            펜마다의 고유 기능이 발현된다.
    */

    public void fillColor(Pen p) {
        p.draw();
    }
    
    public void drawShape(Pen p) {
        p.draw();
    }
}

 

public class PainterApp {
    public static void main(String[] args) {
        
        Painter 그림판 = new Painter();

        BluePen pen1 = new BluePen();
        Redpen pen2 = new Redpen();

        그림판.drawShape(pen1);
        그림판.drawShape(pen2);

    }
}

 

public class PhotoShop {
    private Pen pen;

    public PhotoShop() {
    
    }
    
    // Setter 메소드
    public void setPen(Pen pen) {
        this.pen = pen;
    }

    // 도형 그리기 기능
    public void drawShape() {
        pen.draw();
    }
}

 

public class PhotoShopApp {
    public static void main(String[] args) {
    
    PhotoShop ppoShop = new PhotoShop();

    BluePen pen1 = new BluePen();
    ppoShop.setPen(pen1);

    ppoShop.drawShape();

    }
}

'자바 > oop4' 카테고리의 다른 글

Camera, Phone, Car.java  (0) 2019.06.07
public class Camera {
    public void picture() {
        System.out.println("사진을 찍습니다.");
    }

    public void save() {
        System.out.println("사진을 저장합니다.");
    }

    public void delete() {
        System.out.println("사진을 삭제합니다.");
    }
}

 

public class WebCamera extends Camera {
    
    // 메소드 재정의
    public void save() {
        System.out.println("클라우드에 사진을 저장.");
    }

    // 메소드 재정의
    public void delete() {
        System.out.println("클라우드에서 사진을 삭제.");
    }
}

 

public class CameraApp {
    public static void main(String[] args) {
    
        WebCamera c = new WebCamera();

        c.picture();    // 부모로부터 상속받은 기능 사용
        c.save();        // WebCamera에 재정의된 기능 사용
        c.delete();        // WebCamera에 재정의된 기능 사용
    }
}

 

public class Phone {
    String tel;

    public void connect() {
        System.out.println(tel + "에서 전화를 겁니다.");
    }

    public void disconnect() {
        System.out.println(tel + "에서 전화를 끊습니다.");
    }
}

 

public class SmartPhone extends Phone {

    String ipAddress;

    void facetime() {
        System.out.println(tel + "로 화상통화를 시작합니다.");
    }

    void internet() {
        System.out.println(ipAddress + "로 인터넷에 접속합니다.");
    }
}

 

public class PhoneApp {
    public static void main(String[] args) {
    
        Phone p1 = new Phone();
        p1.tel = ("010-2345-5678");
        p1.connect();
        p1.disconnect();
        System.out.println();

        SmartPhone p2 = new SmartPhone();
        p2.tel = ("010-1111-2222");
        p2.connect();
        p2.disconnect();
        p2.ipAddress = ("192.168.10.254");
        p2.internet();
        p2.facetime();
    }
}

 

public class Car {
    // 자식 클래스에 상속되지 않음
    private String name;
    private int speed;
    
    public Car() {
        super();
        System.out.println("Car() 생성자가 실행됨");
    }

    public Car(String name, int speed) {
        super();
        this.name = name;
        this.speed = speed;
        System.out.println("Car(String, int) 생성자 실행됨");
    }

    // Getter/Setter 자식 클래스에 상속됨
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    // 자식 클래스에 상속됨 (public 접근 제한이 적용된 필드와 메소드)
    public void Start() {
        System.out.println("출발한다.");
    }
    public void Stop() {
        System.out.println("멈춘다.");
    }

    public void carInfo() {
        System.out.println("### 차량 정보 ###");
        System.out.println("모델명 : " + name);
        System.out.println("속  도 : " + speed);
    }
}

 

public class Sonata extends Car {

    public Sonata() {
        System.out.println("Sonata() 생성자가 실행됨");
    }

    public void currentSpeed() {
        System.out.println("현재속도 : " + getSpeed());

    }

    public void speedUp() {
        int current = getSpeed();
        setSpeed(current + 20);
    }
}

 

public class CarApp {
    public static void main(String[] args) {
    
        /*
        Sonata 객체를 생성할 때 부모 객체인 Car가
        먼저 생성되고 Sonata 객체가 생성된다.
        */

        Sonata car1 = new Sonata();
        car1.speedUp();
        car1.currentSpeed();

        Sonata car2 = new Sonata();
    }
}

 

public class Genesis extends Car {
    private int price;
    
    public Genesis() {
        super();
        System.out.println("Genesis() 생성자가 실행됨");
    }

    public Genesis(String name, int speed, int price) {
        super(name, speed);
        this.price = price;
        System.out.println("Genesis(String, int, int) 생성자 실행됨");
    }

    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }

    public void carInfo() {
        /*
        System.out.println("### 차량 정보 ###");
        System.out.println("모델명 : " + getName());
        System.out.println("속  도 : " + getSpeed());
        */
        super.carInfo(); //    부모 클래스에 정의된 carInfo() 호출
        System.out.println("가  격 : " + price);
    }
}

 

public class CarApp2 {
    public static void main(String[] args) {
        Genesis car1 = new Genesis();
        car1.setName("G90");
        car1.setSpeed(350);
        car1.setPrice(100000000);
        car1.carInfo();

        Genesis car2 = new Genesis("G90L", 400, 150000000);
        car2.carInfo();
    }
}

'자바 > oop4' 카테고리의 다른 글

Tire, PhotoShop.java  (0) 2019.06.07

+ Recent posts