Skip to content

Commit

Permalink
[Week3][Chap11] 다형성 - 역할과 구현 예제3 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Feb 13, 2024
1 parent a28530e commit 0662b6f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/car1/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package week3.poly.car1;

public interface Car {
void startEngine();
void offEngine();
void pressAccelerator();
}
22 changes: 22 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/car1/CarMain1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package week3.poly.car1;

public class CarMain1 {
public static void main(String[] args) {
Driver driver = new Driver();

// k3 선택
K3Car k3Car = new K3Car();
driver.setCar(k3Car);
driver.drive();

// 차량 변경(k3 -> model3)
Model3Car model3Car = new Model3Car();
driver.setCar(model3Car);
driver.drive();

//차량 변경(model3 -> newCar)
NewCar newCar = new NewCar();
driver.setCar(newCar);
driver.drive();
}
}
18 changes: 18 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/car1/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package week3.poly.car1;

public class Driver {

private Car car;

public void setCar(Car car) {
System.out.println("자동차를 설정합니다: " + car);
this.car = car;
}

public void drive() {
System.out.println("자동차를 운전합니다.");
car.startEngine();
car.pressAccelerator();
car.offEngine();
}
}
18 changes: 18 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/car1/K3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package week3.poly.car1;

public class K3Car implements Car {
@Override
public void startEngine() {
System.out.println("K3Car.startEngine");
}

@Override
public void offEngine() {
System.out.println("K3Car.offEngine");
}

@Override
public void pressAccelerator() {
System.out.println("K3Car.pressAccelerator");
}
}
18 changes: 18 additions & 0 deletions 임준형/java-basic/src/main/java/week3/poly/car1/Model3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package week3.poly.car1;

public class Model3Car implements Car {
@Override
public void startEngine() {
System.out.println("Model3Car.startEngine");
}

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

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

0 comments on commit 0662b6f

Please sign in to comment.