定義車類Vehicle,小車類和自行車類Bicycle,注意父類和子類之間的關(guān)系。其中,Vehicle類的成員變量包括車輪數(shù)和載人數(shù),成員方法包括停止方法,通過多態(tài)星,完成本程序。
2 回答
已采納

frece
TA貢獻(xiàn)36條經(jīng)驗(yàn) 獲得超15個(gè)贊
public?class?Test?{ public?static?void?main(String[]?args)?{ Vehicle?vehicle?=?new?Vehicle(); Vehicle?car?=?new?Car(); Vehicle?bike?=?new?Bicycle(); vehicle.stop(); car.stop(); bike.stop(); /** ?*?輸出結(jié)果: ?*?Vehicle?Stop,?numOfWheel:?0?maxPerson:?0 ?* Car?Stop,?numOfWheel:?4?maxPerson:?5 ?* Bicycle?Stop,?numOfWheel:?2?maxPerson:?2 ?* ?*/ } } class?Vehicle{ //車輪數(shù) public?int?numOfWheel; //最大載人數(shù) public?int?maxPerson; //停止方法 public?void?stop()?{ System.out.println("Vehicle?Stop,?numOfWheel:?"?+?numOfWheel?+?"?maxPerson:?"?+?maxPerson); } } class?Car?extends?Vehicle{ Car(){ //設(shè)置屬性 this.numOfWheel?=?4; this.maxPerson?=?5; } @Override //重寫停止方法 public?void?stop()?{ System.out.println("Car?Stop,?numOfWheel:?"?+?numOfWheel?+?"?maxPerson:?"?+?maxPerson); } } class?Bicycle?extends?Vehicle{ Bicycle(){ //設(shè)置屬性 this.numOfWheel?=?2; this.maxPerson?=?2; } @Override //重寫停止方法 public?void?stop()?{ System.out.println("Bicycle?Stop,?numOfWheel:?"?+?numOfWheel?+?"?maxPerson:?"?+?maxPerson); } }
添加回答
舉報(bào)
0/150
提交
取消