请问老师代码有哪里需要优化的吗?

来源:2-23 编程练习

小老哥丶

2019-09-26 15:41:57

1、在子类调用父类特有的属性或方法,是使用this还是使用super更规范呢?


2、请问下面代码有哪里需要优化的吗?

public class Test {
    public static void main(String[] args) {
		System.out.print("父类信息测试:");
		NonMotor n=new NonMotor("天宇","红",4,2);
        System.out.println(n.work());
		System.out.print("自行车类信息测试:");
        Bicycle b=new Bicycle("黄","捷安特");
        System.out.println(b.work());
		System.out.print("电动车类信息测试:");
        ElectricVehicle e=new ElectricVehicle("飞鸽");
        System.out.println(e.work());
		System.out.print("三轮车类信息测试:");
		Tricycle t=new Tricycle();
		System.out.println(t.work());
	}
}
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 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 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;
	}
}
public class Bicycle extends NonMotor {
    // 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(){
 
}
public Bicycle(String color,String brand)
{
   super(brand,color);
}
	// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
    public String work(){
        String str="这是一辆"+this.getColor()+"颜色的,"+this.getBrand()+"牌的自行车。";
        return str;
    }
    
}
public class ElectricVehicle extends NonMotor {
    // 私有属性:电池品牌
	private String batteryBrand;


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


写回答

1回答

好帮手慕酷酷

2019-09-26

同学你好,程序完成的很棒!继续努力!不需要优化啦~

调用属性或方法时,建议使用this关键字,更加符合规范哦~

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 11489 问题

查看课程