728x90
문제
아래의 4개 클래스와 1개의 인터페이스를 작성하고, 실행결과를 출력하시오!
Vehicle (인터페이스)
Bus, Taxi, Driver, DriverExample
Vehicle(인터페이스)
멤버함수
void run(void) *추상메서드
void stop(void) *추상메서드
Bus Class(구현클래스)
멤버함수
void run(void)
void stop(void)
Taxi Class(구현클래스)
멤버함수
void run(void)
void stop(void)
Driver(독립클래스)
멤버함수
void drive(Vehicle)
void stop(Vehicle)
DriverExample(실행클래스)
결과
버스가 달립니다.
버스가 멈춥니다.
택시가 달립니다.
택시가 멈춥니다.
코드
interface Vehicle {
void run();
void stop();
}
class Bus implements Vehicle {
public void run() {
System.out.println("버스가 달립니다.");
}
public void stop() {
System.out.println("버스가 멈춥니다.");
}
}
class Taxi implements Vehicle {
public void run() {
System.out.println("택시가 달립니다.");
}
public void stop() {
System.out.println("택시가 달립니다.");
}
}
class Driver {
void drive(Vehicle v) {
v.run();
}
void stop(Vehicle v) {
v.stop();
}
}
class DriverExample {
public static void main(String[] args) {
Bus bus = new Bus();
Taxi taxi = new Taxi();
Driver driver = new Driver();
driver.drive(bus);
driver.stop(bus);
driver.drive(taxi);
driver.stop(taxi);
}
}728x90
'Java' 카테고리의 다른 글
| [Java] (interface) (18-1) (0) | 2023.08.30 |
|---|---|
| [Java] (interface) (17-2) (0) | 2023.08.30 |
| [Java] (추상 클래스) (16-2) (0) | 2023.08.30 |
| [Java] (추상 클래스) (16-1) (0) | 2023.08.30 |
| [Java] 클래스 (상속, 오버라이딩, 다형성, 스택, 팝) (15) (0) | 2023.08.27 |