-
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][Chap11] 다형성 - 역할과 구현 예제3 (#26)
- Loading branch information
Showing
5 changed files
with
83 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,7 @@ | ||
package week3.poly.car1; | ||
|
||
public interface Car { | ||
void startEngine(); | ||
void offEngine(); | ||
void pressAccelerator(); | ||
} |
22 changes: 22 additions & 0 deletions
22
임준형/java-basic/src/main/java/week3/poly/car1/CarMain1.java
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,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(); | ||
} | ||
} |
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 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(); | ||
} | ||
} |
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 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
18
임준형/java-basic/src/main/java/week3/poly/car1/Model3Car.java
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 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"); | ||
} | ||
} |