标签:Road 软件设计 番外 void transport new public Transport
用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。
实验要求:
1. 画出对应的类图;
2.提交源代码;
package test9;
public class Bus implements Transport{
@Override
public void run() {
System.out.print("Bus");
}
}
package test9;
public class Car implements Transport{
@Override
public void run() {
System.out.print("Car");
}
}
package test9;
public class LIqing extends Road{
public void drive() {
transport.run();
System.out.println("行驶在沥青路");
}
}
package test9;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Transport transport = null;
Road road = null;
System.out.println("请选择:1、Car/水泥路 2、Car/沥青路 3、Bus/水泥路 4、Bus/沥青路");
Scanner input=new Scanner(System.in);
int i=input.nextInt();
if(i==1) {
transport=(Transport)new Car();
road=(Road)new Shuini();
}else if(i==2) {
transport=(Transport)new Car();
road=(Road)new LIqing();
}else if(i==3){
transport=(Transport)new Bus();
road=(Road)new Shuini();
}else if(i==4) {
transport=(Transport)new Bus();
road=(Road)new LIqing();
}else {
System.out.println("输入无效!");
}
road.setTransport(transport);
road.drive();
}
}
package test9;
public abstract class Road {
protected Transport transport;
public void setTransport(Transport tr) {
this.transport=tr;
}
public abstract void drive();
}
package test9;
public class Shuini extends Road{
public void drive() {
transport.run();
System.out.println("行驶在水泥路");
}
}
package test9;
public interface Transport {
public void run();
}
3.注意编程规范。
标签:Road,
软件设计,
番外,
void,
transport,
new,
public,
Transport
From: https://www.cnblogs.com/zjsdbk/p/17811826.html