我是不是想太简单了?关于课后习题
1 2 3 4 5 6 7 | //父类 Vehicle· package com.imooc; public class Vehicle { public void show(){ System.out.println( "可以载多少人呢?" ); } } |
1 2 3 4 5 6 7 8 9 10 11 | //子类 Taxi package com.imooc; public class Taxi extends Vehicle { public void show(){ String numb= "1~4人" ; String way= "马路" ; System.out.println( "Taxi:" ); System.out.println( "一般可载:" + numb); System.out.println( "工作媒介:" + way); } } |
1 2 3 4 5 6 7 8 9 10 11 | //子类 Bus package com.imooc; public class Bus extends Vehicle { public void show(){ String numb= "1~50人" ; String way= "马路" ; System.out.println( "Bus:" ); System.out.println( "一般可载:" + numb); System.out.println( "工作媒介:" + way); } } |
1 2 3 4 5 6 7 8 9 10 11 12 | //测试的Initial package com.imooc; public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Vehicle obj1 = new Taxi(); Vehicle obj2 = new Bus(); obj1.show(); obj2.show(); } } |
子类Initial输出的结果为:
1 2 3 4 5 6 | Taxi: 一般可载: 1 ~ 4 人 工作媒介:马路 Bus: 一般可载: 1 ~ 50 人 工作媒介:马路 |