老师帮忙看一下
package com.imooc.exercises;
public class NonMotor {
private String brand;// 品牌
private String colour;// 颜色
private int wheel = 2;// 轮子
private int chair = 1;// 座椅
// 无参构造
public NonMotor() {
}
// 双参构造 对品牌、颜色赋值
public NonMotor(String brand, String colour) {
this.setBrand(brand);
this.setColour(colour);
}
// 四参构造 对所有属性赋值
public NonMotor(String brand, String colour, int wheel, int chair) {
this.setBrand(brand);
this.setColour(colour);
this.setWheel(wheel) ;
this.setChair(chair);
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
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 = "这是一辆" + getColour() + "颜色的," + getBrand() + "牌的非机动车,有" + getWheel() + "轮子,有" + getChair() + "个座椅";
return str;
}
}
===============================
package com.imooc.exercises;
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(){
super("捷安特","黄");
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work(){
String str = "这是一辆"+this.getColour()+"颜色的,"+this.getBrand()+"牌的自行车。";
return str;
}
}
======================
package com.imooc.exercises;
public class ElectricVehicle extends NonMotor{
// 私有属性:电池品牌
public String batteryBrand;
// 公有的get***/set***方法完成属性封装
public String getBatteryBrand(){
return this.batteryBrand;
}
public void setBatteryBrand(String batteryBrand){
this.batteryBrand=batteryBrand;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work(){
String str = "这是一辆使用"+getBatteryBrand()+"牌电池的电动车。";
return str;
}
}
=====================
package com.imooc.exercises;
public class Tricycle extends NonMotor{
// 在无参构造中实现对轮子属性值进行修改
public Tricycle(){
super.setWheel(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work(){
String str = "三轮车是一款有"+this.getWheel()+"个轮子的非机动车。";
return str;
}
}
==============
package com.imooc.test;
import com.imooc.exercises.*;
public class Test {
public static void main(String[]args){
System.out.print("父类信息测试:");
NonMotor non = new NonMotor("天宇牌","红",4,2);
System.out.println(non.work());
System.out.print("自行车类信息测试:");
Bicycle bic = new Bicycle();
System.out.println(bic.work());
System.out.print("电动车类信息测试:");
ElectricVehicle ele = new ElectricVehicle();
ele.setBatteryBrand("飞鸽");
System.out.println(ele.work());
System.out.print("三轮车类信息测试:");
Tricycle tri = new Tricycle();
System.out.println(tri.work());
}
}0
收起
正在回答 回答被采纳积分+1
1回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星