Skip to content

Commit

Permalink
[Week3][Chap11] 다형성 - 역할과 구현 예제2 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Feb 13, 2024
1 parent 28f3dbd commit a28530e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public static void main(String[] args) {
driver.setK3Car(k3Car);
driver.drive();

// 추가
Model3Car model3Car = new Model3Car();
driver.setK3Car(null);
driver.setModel3Car(model3Car);
driver.drive();
}
}
17 changes: 14 additions & 3 deletions 임준형/java-basic/src/main/java/week3/poly/car0/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
public class Driver {

private K3Car k3Car;
private Model3Car model3Car; // 추가

public void setK3Car(K3Car k3Car) {
this.k3Car = k3Car;
}

public void setModel3Car(Model3Car model3Car) {
this.model3Car = model3Car;
}

public void drive() {
System.out.println("자동차를 운전합니다.");
k3Car.startEngine();
k3Car.pressAccelerator();
k3Car.offEngine();
if (k3Car != null) {
k3Car.startEngine();
k3Car.pressAccelerator();
k3Car.offEngine();
} else if (model3Car != null) {
model3Car.startEngine();
model3Car.pressAccelerator();
model3Car.offEngine();
}
}

}
16 changes: 16 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/car0/Model3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package week3.poly.car0;

public class Model3Car {

public void startEngine() {
System.out.println("Model3Car.startEngine");
}

public void offEngine() {
System.out.println("Model3Car.offEngine");
}

public void pressAccelerator() {
System.out.println("Model3Car.pressAccelerator");
}
}

0 comments on commit a28530e

Please sign in to comment.