
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
3-12 강의 내용 캡쳐입니다.
외부에서 객체의 private 한 필드를 저장/수정할 필요가 있을 때 Setter 메서드를 사용한다고 배웠습니다.
그런데 여기에선 타이어 클래스에 모두 Public으로 되어있는데 왜 Car.java나 Main.java에서 setTire라고 접근했는지 의문입니다.

작성한 코드 및 에러 메세지
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 + " 방향으로 핸들을 돌립니다.");
}
}
public class Car {
static final String company = "GENESIS"; // 자동차 회사String model; // 자동차 모델String color; // 자동차 색상
double price; // 자동차 가격
double speed; // 자동차 속도 , km/h
char gear = 'P'; // 기어의 상태, P,R,N,Dboolean 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 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");
}
}
