JAVA

[JAVA] 클래스간의 관계

개발 공주 2023. 6. 1. 07:00
728x90

포함 관계

포함관계는 상속과 같은 방식으로 관계를 맺는 게 아닌, 클래스 내에서 다른 클래스의 인스턴스를 생성하면 포함 관계라고 볼 수 있다.

 

class Car {
    Door b = new Door();
}

Car 클래스에서 Door 클래스의 인스턴스를 생성한다. 이렇게 클래스 내부에서 다른 클래스의 인스턴스를 생성하면 포함관계라고 볼 수 있다.

 

관계 설정

📌 클래스간의 관계를 분석하여 관계설정을 해줄 수 있습니다.

  • 상속관계 : is - a (”~은 ~(이)다”)
  • 포함관계 : has - a (”~은 ~을(를) 가지고 있다”)

ex) 상속관계

  • 상속관계 : 고래는 포유류다 👍
  • 포함관계 : 고래는 포유류를 가지고 있다…? 🤔

ex) 포함관계

  • 자동차는 타이어를 가지고 있다. 👍
  • 자동차는 차문을 가지고 있다. 👍
  • 자동차는 핸들을 가지고 있다. 👍

코드 예시

자동차 클래스

public class Car {

    static final String company = "GENESIS"; // 자동차 회사
    String model; // 자동차 모델
    String color; // 자동차 색상
    double price; // 자동차 가격

    double speed;  // 자동차 속도 , km/h
    char gear = 'P'; // 기어의 상태, P,R,N,D
    boolean lights; // 자동차 조명의 상태

    Tire[] tire;
    Door[] door;
    Handle handle;

    public Car(String model, String color, double price) {
        this.model = model;
        this.color = color;
        this.price = price;
    }

    public void setTire(Tire ... tire) {
        this.tire = tire;
    }

    public void setDoor(Door ... door) {
        this.door = door;
    }

    public void setHandle(Handle handle) {
        this.handle = handle;
    }

    public double gasPedal(double kmh, char type) {
        changeGear(type);
        speed = kmh;
        return speed;
    }

    public double brakePedal() {
        speed = 0;
        return speed;
    }

    public char changeGear(char type) {
        gear = type;
        return gear;
    }

    public boolean onOffLights() {
        lights = !lights;
        return lights;
    }

    public void horn() {
        System.out.println("빵빵");
    }
}

핸들,차문,타이어 클래스

public class Tire {
    String company; // 타이어 회사
    double price; // 타이어 가격

    public Tire(String company, double price) {
        this.company = company;
        this.price = price;
    }
}

public class Door {
    String company; // 차문 회사
    String location; // 차문 위치, FL, FR, BL, BR

    public Door(String company, String location) {
        this.company = company;
        this.location = location;
    }
}

public class Handle {
    String company; // 핸들 회사
    String type; // 핸들 타입

    public Handle(String company, String type) {
        this.company = company;
        this.type = type;
    }

    public void turnHandle(String direction) {
        System.out.println(direction + " 방향으로 핸들을 돌립니다.");
    }
}

Main 클래스

public class Main {
    public static void main(String[] args) {
        // 자동차 객체 생성
        Car car = new Car("GV80", "Black", 50000000);

        // 자동차 부품 : 타이어, 차문, 핸들 선언
        Tire[] tires = new Tire[]{
                new Tire("KIA", 150000), new Tire("금호", 150000),
                new Tire("Samsung", 150000), new Tire("LG", 150000)
        };
        Door[] doors = new Door[]{
                new Door("LG", "FL"), new Door("KIA", "FR"),
                new Door("Samsung", "BL"), new Door("LG", "BR")
        };
        Handle handle = new Handle("Samsung", "S");


        // 자동차 객체에 부품 등록
        car.setTire(tires);
        car.setDoor(doors);
        car.setHandle(handle);

        // 등록된 부품 확인하기
        for (Tire tire : car.tire) {
            System.out.println("tire.company = " + tire.company);
        }
        System.out.println();

        for (Door door : car.door) {
            System.out.println("door.company = " + door.company);
            System.out.println("door.location = " + door.location);
            System.out.println();
        }
        System.out.println();

        // 자동차 핸들 인스턴스 참조형 변수에 저장
        Handle carHandle = car.handle;
        System.out.println("carHandle.company = " + carHandle.company);
        System.out.println("carHandle.type = " + carHandle.type + "\n");

        // 자동차 핸들 조작해보기
        carHandle.turnHandle("Right");
        carHandle.turnHandle("Left");
    }
}

'JAVA' 카테고리의 다른 글

[JAVA] Object  (0) 2023.06.01
[JAVA] final 클래스와 final 메서드  (0) 2023.06.01
[JAVA] 상속 개념 및 예시  (0) 2023.05.31
[JAVA] 접근 제어자  (0) 2023.05.31
[JAVA] this 와 this()  (0) 2023.05.31