最后是按老师的意思输出了,但请问有更简洁的代码吗?
public class Test {
public static void main(String[] args) {
Vehicle bike=new Bike();
Vehicle bus=new Bus();
Vehicle metro=new Metro();
Vehicle plane=new Plane();
Vehicle ship=new Ship();
bike.sittingPeople=1;
bike.wayOfTranstpotaiton="road";
bus.sittingPeople=40;
bus.wayOfTranstpotaiton="road";
metro.sittingPeople=1000;
metro.wayOfTranstpotaiton="road";
plane.sittingPeople=300;
plane.wayOfTranstpotaiton="sky";
ship.sittingPeople=800;
ship.wayOfTranstpotaiton="sea";
bike.feature();
bus.feature();
metro.feature();
plane.feature();
ship.feature();
}
}我创建了5个交通工具,并且通过了父类对象引用赋值及其方法,最后都能够输入:
巴士特性:人数:40运输方式:road
地铁特性:人数:1000运输方式:road
飞机特性:人数:300运输方式:sky
轮船特性:人数:800运输方式:sea
但是这样的写法太啰嗦,不知道有没有同学有更简洁的代码供参考?