package ch06;
public class Car {
public static void main(String[] args) {
String name;
int price;
//참조 타입 (포함 관계)
Engine engine;
public Car(String name, int price) {
this.name = name;
this.price = price;
this.engine = new Engine("GDI",500);
}
public void showInfo() {
System.out.println("name :" + name);
System.out.println("price :" + price);
}
}
}
package ch06;
public class Engine {
String name;
int price;
public Engine(String name, int price) {
this.name = name;
this.price = price;
// 생성자 만들기 , 리턴타입이 없다
}
public void showInfo() {
System.out.println("name : " + name);
System.out.println("price : " + price);
}
}
package ch06;
public class CarMainTest {
public static void main(String[] args) {
Car car = new Car("제네시스",5000);
car.showInfo();
System.out.println(car.engine.name);
System.out.println(car.engine.price);
System.out.println(car.engine);
}
}
'Java > chapter2' 카테고리의 다른 글
ch07 Hero (0) | 2021.08.25 |
---|---|
ch07 Bank (0) | 2021.08.25 |
ch05 별모양 역으로 5->1 (0) | 2021.08.25 |
ch05 Student (0) | 2021.08.25 |
ch04 Student Bus Subway (0) | 2021.08.25 |