课题打卡~请老师检查
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;
private String color;
private int wheel = 2;
private int chair = 1;
// 无参构造方法
public NonMotor() {
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String brand, String color) {
this.setBrand(brand);
this.setColor(color);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String color,String brand,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;
}
}
=====================================================================
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle() {
}
public Bicycle(String color, String brand) {
super(color, brand);
this.setColor(color);
this.setBrand(brand);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str = "这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的自行车";
return str;
}
}
=====================================================================
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String Battery;
public ElectricVehicle() {
}
public ElectricVehicle(String battery) {
this.setBattery(battery);
}
// 公有的get***/set***方法完成属性封装
public String getBattery() {
return Battery;
}
public void setBattery(String battery) {
Battery = battery;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str = "这是一辆使用"+this.getBattery()+"牌电池的电动车";
return str;
}
}
=====================================================================
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle() {
super();
this.setWheel(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str = "三轮车是一款有"+this.getWheel()+"个轮子的非机动车";
return str;
}
}
=====================================================================
public class Test {
public static void main(String[] args) {
//测试
NonMotor motor = new NonMotor("红","天宇",4,2);
System.out.print("父类信息测试:"+motor.work()+"\n");
Bicycle bicy = new Bicycle("黄","捷安特");
System.out.print("自行车类信息测试:"+bicy.work()+"\n");
ElectricVehicle ev = new ElectricVehicle ("飞鸽");
System.out.print("电动车类信息测试:"+ev.work()+"\n");
Tricycle tr = new Tricycle();
System.out.print("三轮车类信息测试:"+tr.work()+"\n");
}
}7
收起
正在回答
1回答
同学你好,程序完成的不错!但还有一个小问题:
当方法或变量名由一个单词组成时,则该单词均小写。当由多个单词组成时,第一个单词所有字母均小写,从第二个单词开始,每个单词的首字母大写。如:Battery应改为:battery
祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星