本节编程我代码对吧

本节编程我代码对吧

package com.gongsi.zixingche;


public class Test {


public static void main(String[] args) {

System.out.print("父类信息测试:");

NonMotor n=new NonMotor("天宇牌","红",2,3);

System.out.println(n.work());

System.out.print("自行车类信息测试:");

Bicycle bi=new Bicycle("捷安特","黄");

System.out.println(bi.work());

System.out.print("电动车类信息测试:");

ElectricVehicle el=new ElectricVehicle("飞鸽");

System.out.println(el.work());

System.out.print("三轮车类信息测试:");

Tricycle tr=new Tricycle();

tr.setWheelNonMotor(3);

System.out.println(tr.work());

}


}


package com.gongsi.zixingche;

/**

 * 非机动车父类

 */

public class NonMotor {

// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)

private String brandNonMotor; //品牌

private String colourNonMotor; //颜色

private int wheelNonMotor=2;  //轮子

private int chairNonMotor=1;//座椅

// 无参构造方法

public NonMotor() {

}

// 双参构造方法,完成对品牌和颜色的赋值

public NonMotor(String brandNonMotor, String colourNonMotor) {

this.brandNonMotor = brandNonMotor;

this.colourNonMotor = colourNonMotor;

}

// 四参构造方法,分别对所有属性赋值

public NonMotor(String brandNonMotor, String colourNonMotor, int wheelNonMotor, int chairNonMotor) {

this.brandNonMotor = brandNonMotor;

this.colourNonMotor = colourNonMotor;

this.wheelNonMotor = wheelNonMotor;

this.chairNonMotor = chairNonMotor;

}

   // 公有的get***/set***方法完成属性封装

public String getBrandNonMotor() {

return brandNonMotor;

}

public void setBrandNonMotor(String brandNonMotor) {

this.brandNonMotor = brandNonMotor;

}

public String getColourNonMotor() {

return colourNonMotor;

}

public void setColourNonMotor(String colourNonMotor) {

this.colourNonMotor = colourNonMotor;

}

public int getWheelNonMotor() {

return wheelNonMotor;

}

public void setWheelNonMotor(int wheelNonMotor) {

this.wheelNonMotor = wheelNonMotor;

}

public int getChairNonMotor() {

return chairNonMotor;

}

public void setChairNonMotor(int chairNonMotor) {

this.chairNonMotor = chairNonMotor;

}

// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供

public String work() {

String str="这是一辆"+this.getColourNonMotor()+"颜色的,"+this.getBrandNonMotor()+"牌的非机动车"+",有"+this.getChairNonMotor()+"个轮子,"+"有"+this.getWheelNonMotor()+"个座椅";

return str;

}


}


package com.gongsi.zixingche;

/**

 * 自行车类

 * @author xixi

 *

 */

public class Bicycle extends NonMotor{

// 在构造方法中调用父类多参构造,完成属性赋值

public Bicycle(String brandNonMotor, String colourNonMotor) {

super(brandNonMotor,colourNonMotor);

}

// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供

public String work() {

String str1="这是一辆"+this.getColourNonMotor()+"颜色的,"+this.getBrandNonMotor()+"牌的自行车";

return str1;

}

  

}


package com.gongsi.zixingche;

/**

* 电动汽车类

*/


public class ElectricVehicle extends NonMotor {

private String batteryElectricVehicle; //电池品牌

    // 公有的get***/set***方法完成属性封装


public ElectricVehicle(String batteryElectricVehicle) {

this.batteryElectricVehicle = batteryElectricVehicle;

}


public String getBatteryElectricVehicle() {

return batteryElectricVehicle;

}


public void setBatteryElectricVehicle(String batteryElectricVehicle) {

this.batteryElectricVehicle = batteryElectricVehicle;

}


// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供

public String work() {

String str2="这是一辆使用"+this.getBatteryElectricVehicle()+"牌电池的电动车";

return str2;

}



}


package com.gongsi.zixingche;

/**

 * 三轮车类

 * @author xixi

 *

 */

public class Tricycle extends NonMotor{

// 在无参构造中实现对轮子属性值进行修改

public Tricycle() {

}

// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供

public String work() {

String str3="三轮车是一款有"+this.getWheelNonMotor()+"个轮子的非机动车";

return str3;

}

}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好~

同学本节编程实现的没有问题哦。代码写的很规范,棒棒哒,加油!

祝:学习愉快~


问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师