2-18的编程练习,总觉得有哪不妥。老湿嫩帮看看有什么要改进的么?
package perk.tong.imooc.bick;
/**
* 非机动车类,作为父类 属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个) 方法: 1.编写无参构造方法、双参构造方法和四参构造方法,其中,
* 在双参构造方法中,完成对品牌和颜色的赋值; 在四参构造方法中,完成对所有属性的赋值
* 2.编写运行的方法,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。 其中**的数据由属性提供
*
* @author gelid
*
*/
public class Vehicle {
private String brand;// 品牌
private String colour;// 颜色
private int wheel;// 轮子个数(默认2个)
private int seat;// 座椅个数(默认一个)
// 作为父类要有无参构造方法
public Vehicle() {
}
// 创建2个参数的构造方法,品牌、颜色
public Vehicle(String brand, String colour) {
this.setBrand(brand);
this.setColour(colour);
}
// 创建4个参数的构造方法,品牌、颜色、车轮数、座位数
public Vehicle(String brand, String colour, int wheel, int seat) {
this.setBrand(brand);
this.setColour(colour);
this.setWheel(wheel);
this.setSeat(seat);
}
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) {
// 设置默认车轮数2个
if (wheel < 2)
this.wheel = 2;
else
this.wheel = wheel;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
// 设置默认座位1个
if (seat < 1)
this.seat = 1;
else
this.seat = seat;
}
public String info() {
String str = "父类信息测试:这是一辆" + this.getColour() + "色的," + this.getBrand()
+ "牌的非机动车,有" + this.getWheel() + "个轮子,有" + this.getSeat()
+ "个座椅。";
return str;
}
}
正在回答
Vehicle类中,轮子个数默认为2个,座椅默认为1个,也就是在定义变量的时候直接赋值就可以了,不用在set方法里进行设置。
如:private int wheel=2;// 轮子个数(默认2个)
其他内容没问题。祝学习愉快!
//Test类:
public class Test {
public static void main(String[] args) {
System.out.print("父类信息测试:");
NonMotor mm = new NonMotor("天宇","红",4,2);
System.out.println(mm.work());
System.out.print("自行车类信息测试:");
Bicycle bc = new Bicycle("捷安特","黄");
System.out.println(bc.work());
System.out.print("电动车类信息测试:");
ElectricVehicle ev = new ElectricVehicle("飞鸽");
System.out.println(ev.work());
System.out.print("三轮车类信息测试:");
Tricycle tc = new Tricycle();
System.out.println(tc.work());
}
}
//NonMotor类:
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String brand;
private String color;
private int wheel;
private int chair;
// 无参构造方法
public NonMotor(){
}
// 双参构造方法,完成对品牌和颜色的赋值
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 this.brand;
}
public void setBrand(String brand){
this.brand = brand;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color = color;
}
public int getWheel(){
return this.wheel;
}
public void setWheel(int wheel){
this.wheel = wheel;
}
public int getChair(){
return this.chair;
}
public void setChair(int chair){
this.chair = chair;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str = "这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的非机动车,有"+this.getWheel()+"个轮子,有"+this.getChair()+"个座椅的非机动车。";
return str;
}
}
//Bicycle类:
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
private String brand;
private String color;
// 无参构造方法
public Bicycle(){
}
public Bicycle(String brand,String color){
super(brand,color);
}
public String getBrand(){
return this.brand;
}
public void setBrand(String brand){
this.brand = brand;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color = color;
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str = "这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的自行车。";
return str;
}
}
//ElectricVehicle类:
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String batteryBrand;
// 无参构造方法
public ElectricVehicle(){
}
public ElectricVehicle(String batteryBrand){
this.setBatteryBrand(batteryBrand);
}
// 公有的get***/set***方法完成属性封装
public String getBatteryBrand(){
return this.batteryBrand;
}
public void setBatteryBrand(String batteryBrand){
this.batteryBrand = batteryBrand;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str = "这是一辆使用"+this.getBatteryBrand()+"牌电池的电动车。";
return str;
}
}
//Tricycle类:
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
private int wheel;
public Tricycle(){
super.setWheel(3);
}
public int getWheel(){
return this.wheel;
}
public void setWheel(int wheel){
this.wheel = wheel;
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str = "三轮车是一款有"+super.getWheel()+"个轮子的非机动车。";
return str;
}
}package taskOne;
import perk.tong.imooc.bick.Bicycle;
import perk.tong.imooc.bick.Electrombile;
import perk.tong.imooc.bick.Tricycle;
import perk.tong.imooc.bick.Vehicle;
/**
* 非机动车测试类
* @author gelid
*
*/
public class VehicleTest {
public static void main(String[] args) {
//父类信息测试
Vehicle ve=new Vehicle("天宇","红色",4,2);
System.out.println(ve.info());
Bicycle by=new Bicycle("捷安特","黄色");
System.out.println(by.info());
Electrombile el=new Electrombile("飞鹤");
System.out.println(el.info());
Tricycle tr=new Tricycle();
System.out.println(tr.info());
}
}
package perk.tong.imooc.bick;
/**
* 电动车类
* @author gelid
*
*/
public class Electrombile extends Vehicle {
private String batteryBrand;
//构造方法
public Electrombile(){
}
public Electrombile(String batteryBrand){
this.setBatteryBrand(batteryBrand);
}
public String getBatteryBrand(){
return batteryBrand;
}
public void setBatteryBrand(String batteryBrand){
this.batteryBrand=batteryBrand;
}
public String info(){
String str="电动车信息测试:这是一台用"+this.getBatteryBrand()+"牌电池的电动车。";
return str;
}
}
package perk.tong.imooc.bick;
/**
* 自行车类
* @author gelid
*
*/
public class Bicycle extends Vehicle{
public Bicycle(){
}
public Bicycle(String brand, String colour){
super(brand,colour);
}
public String info(){
String str="自行车信息测试:这是一辆"+this.getColour()+"的,"+this.getBrand()+"牌自行车。";
return str;
}
}
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星