Skip to content

Commit

Permalink
[Week3][Chap12] 다형성 - 역할과 구현 예제3 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Feb 6, 2024
1 parent cf23e07 commit 993800e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 김아린/java-basic/src/poly/car1/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package poly.car1;

public interface Car {
void startEngine();

void offEngine();

void pressAccelerator();
}
17 changes: 17 additions & 0 deletions 김아린/java-basic/src/poly/car1/CarMain1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package 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();
}
}
17 changes: 17 additions & 0 deletions 김아린/java-basic/src/poly/car1/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package 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/poly/car1/K3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package 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/poly/car1/Model3Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package 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 993800e

Please sign in to comment.