|
publicclassTest{ publicstaticvoidmain(String[]args){ Vehiclevehicle=newVehicle(); Vehiclecar=newCar(); Vehiclebike=newBicycle(); vehicle.stop(); car.stop(); bike.stop(); /** *输出结果: *VehicleStop,numOfWheel:0maxPerson:0 * CarStop,numOfWheel:4maxPerson:5 * BicycleStop,numOfWheel:2maxPerson:2 * */ } }classVehicle{ //车轮数 publicintnumOfWheel; //最大载人数 publicintmaxPerson; //停止方法 publicvoidstop(){ System.out.println("VehicleStop,numOfWheel:"+numOfWheel+"maxPerson:"+maxPerson); }}classCarextendsVehicle{ Car(){ //设置属性 this.numOfWheel=4; this.maxPerson=5; } @Override //重写停止方法 publicvoidstop(){ System.out.println("CarStop,numOfWheel:"+numOfWheel+"maxPerson:"+maxPerson); }}classBicycleextendsVehicle{ Bicycle(){ //设置属性 this.numOfWheel=2; this.maxPerson=2; } @Override //重写停止方法 publicvoidstop(){ System.out.println("BicycleStop,numOfWheel:"+numOfWheel+"maxPerson:"+maxPerson); }} |