-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Week3][Chap12] 다형성 - 역할과 구현 예제3 (#27)
- Loading branch information
Showing
5 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |