快捷导航

一个java编程问题,我不知该怎么写。

定义车类Vehicle,小车类和自行车类Bicycle,注意父类和子类之间的关系。其中,Vehicle类的成员变量包括车轮数和载人数,成员方法包括停止方法,通过多态星,完成本程序。

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员2

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);        }}
回复

使用道具 举报

看楼上
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP