is-a: ์์
has-a: ~์ ~์ ๊ฐ์ง๊ณ ์๋ค
class Car {
private String maker;
private int price;
private Engine engine; // car has-a Engine
private Point location; // car has-a Point
public Car(String maker, int price, Engine engine, Point location) {
setMaker(maker);
setPrice(price);
setEngine(engine);
setLocation(location);
}
public String getMaker() {
return maker;
}
public int getPrice() {
return price;
}
public Engine getEngine() {
return engine;
}
public Point getLocation() {
return location;
}
public void setMaker(String maker) {
this.maker = maker;
}
public void setPrice(int price) {
this.price = price;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public void setLocation(Point location) {
this.location = location;
}
@Override
public String toString() {
String info = "maker : " + maker + "\n";
info += "price: " + price + "\n";
info += "engine: " + engine + "\n";
info += "location: " + location + "\n";
return info;
}
public void drive(Point goal) {
System.out.println(goal + "๋ก ์ด๋ํฉ๋๋ค.");
engine.startEngine();
setLocation(goal);
}
}
class Ship {
private Engine engine;
private Point location;
}
class Point {
private int x;
private int y;
public Point(int x, int y) {
setX(x);
setY(y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return x + ", " + y;
}
}
class Engine {
private int cc;
private int ps;
public Engine(int cc, int ps) {
set(cc);
set(ps);
}
public int getCc() {
return cc;
}
public int getPs() {
return ps;
}
public void setCc(int cc) {
this.cc = cc;
}
public void setPs(int ps) {
this.ps = ps;
}
public void startEngine() {
System.out.println("์๋์ ๊ฒ๋๋ค.");
}
@Override
public String toString() {
return cc + "cc," + ps + "ps";
}
class UseCar {
public static void main(Strign[] arsg) {
Car c = new car("Benz", 1000000, new Engine(5000, 400), new Point(0,0));
c.drive(new Point(100, 100));
System.out.println(c);
}
}