请老师帮忙看看,有哪些可以改进的地方

请老师帮忙看看,有哪些可以改进的地方

package com.imooc.car;

/**
 * 非机动车
 * 
 * @author Administrator
 *
 */
public class NonMotor {

	// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
	private String brand;
	private String color;
	private int wheel = 2;
	private int chair = 1;

	// 无参构造方法
	public NonMotor() {
		super();
	}

	// 双参构造方法,完成对品牌和颜色的赋值
	public NonMotor(String brand, String color) {
		this.setBrand(brand);
		this.setColor(color);
	}

	// 四参构造方法,分别对所有属性赋值
	public NonMotor(String brand, String color, int wheel, int chair) {
		this.setBrand(brand);
		this.setColor(color);
		this.setWheel(wheel);
		this.setChair(chair);
	}

	// 公有的get***/set***方法完成属性封装
	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getWheel() {
		return wheel;
	}

	public void setWheel(int wheel) {
		this.wheel = wheel;
	}

	public int getChair() {
		return chair;
	}

	public void setChair(int chair) {
		this.chair = chair;
	}

	// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
	public String work() {
		String str = "这是一辆" + this.getColor() + "颜色的," + this.getBrand() + "牌的非机动车,有" + this.getWheel() + "个轮子,有"
				+ this.getChair() + "个座椅的非机动车。";
		return str;
	}

}


package com.imooc.car;

/**
 * 自行车类
 * 
 * @author Administrator
 *
 */
public class Bicycle extends NonMotor {

	// 在构造方法中调用父类多参构造,完成属性赋值
	public Bicycle(String brand,String color){
		super(brand,color);
	}
	// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
	public String work() {
		String str = "这是一辆" + this.getColor() + "颜色的," + this.getBrand() + "牌的自行车。";
		return str;
	}
	
}


package com.imooc.car;
/**
 * 电动车
 */
public class ElectricVehicle extends NonMotor {
	// 私有属性:电池品牌
	private String batteryBrand;
	
	public ElectricVehicle() {
		super();
	}

	// 公有的get***/set***方法完成属性封装
	public String getBatteryBrand() {
		return batteryBrand;
	}

	public void setBatteryBrand(String batteryBrand) {
		this.batteryBrand = batteryBrand;
	}
	// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
	public String work() {
		String str = "这是一辆使用" + this.getBatteryBrand() + "牌电池的电动车。";
		return str;
	}
}


package com.imooc.car;

/**
 * 三轮车类
 * 
 * @author Administrator
 *
 */
public class Tricycle extends NonMotor {

	// 在无参构造中实现对轮子属性值进行修改
	public Tricycle(){
		super.setWheel(3);
	}
	// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
	public String work() {
		String str = "三轮车是一款有" + super.getWheel() + "个轮子的非机动车。";
		return str;
	}
}


package com.imooc.car;

public class Test {

	public static void main(String[] args) {
		System.out.print("父类信息测试:");
		NonMotor motor=new NonMotor("天宇", "红色", 4, 2);
		System.out.println(motor.work());
		System.out.print("自行车类信息测试:");
		Bicycle bicycle=new Bicycle("捷安特", "黄色");
		System.out.println(bicycle.work());
		System.out.print("电动车类信息测试:");
		ElectricVehicle vehicle=new ElectricVehicle();
		vehicle.setBatteryBrand("飞鸽");
		System.out.println(vehicle.work());
		System.out.print("三轮车类信息测试:");
		Tricycle tricycle=new Tricycle();
		System.out.println(tricycle.work());

	}

}


正在回答 回答被采纳积分+1

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

1回答
chrismorgen 2019-03-08 11:50:27

你好同学,效果完成的很棒,加油,祝学习愉快~

  • package com.imooc.car; /** * 三轮车类 * * @author Administrator * */ public class Tricycle extends NonMotor { // 在无参构造中实现对轮子属性值进行修改 public Tricycle(){ super.setWheel(3); } // 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供 public String work() { String str = "三轮车是一款有" + super.getWheel() + "个轮子的非机动车。"; return str; } } 老师,在三轮车类中,此处的super.setWheel(3);改成this.setWheel(3);可以吗?有何区别?
    2019-03-17 21:39:57
  • 你好同学,这两个意思是不一样的哦,super是调用父类的方法,而this是调用本类的方法,祝学习愉快~
    2019-03-18 16:42:41
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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