老师帮我看一下有没有改进的地方

来源:2-21 编程练习

UUU加油

2022-04-10 00:34:56

public class NonMotor {
    // 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
	private String brand;
	private String color;
	public int wheels=2;
	private int seats=1;
	// 无参构造方法
    public NonMotor(){
        
    }
	// 双参构造方法,完成对品牌和颜色的赋值
    public NonMotor(String brand,String color){
        this.setBrand(brand);
        this.setColor(color);
        
    }
	// 四参构造方法,分别对所有属性赋值
   public NonMotor(String brand,String color,int wheels,int seats){
       this.setBrand(brand);
       this.setColor(color);
       this.setWheels(wheels);
       this.setSeats(seats);
   }
   // 公有的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 getWheels(){
       return this.wheels;
   }
   public void setWheels(int wheels){
       this.wheels=wheels;
   }
   public int getSeats(){
       return this.seats;
   }
   public void setSeats(int seats){
       this.seats=seats;
   }
   


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


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

public class ElectricVehicle extends NonMotor {
    // 私有属性:电池品牌
	private String electricBrand;
	public ElectricVehicle(){
	    
	}
	public ElectricVehicle(String electricBrand){
	   
	    this.setElectricBrand(electricBrand);
	    
	}
    // 公有的get***/set***方法完成属性封装
   
   public String getElectricBrand(){
       return this.electricBrand;
   }
   public void setElectricBrand(String electricBrand){
       this.electricBrand=electricBrand;
   }
	// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
	
	public String work() {
	    String str="这是一辆使用"+this.getElectricBrand()+"牌电池的电动车。";
		return str;
	}
}
public class Tricycle extends NonMotor {
    // 在无参构造中实现对轮子属性值进行修改
    public Tricycle(){
        super.wheels=3;
    }
	// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
	public String work() {
	    String str="三轮车是一款有"+super.wheels+"个轮子的非机动车。";
		return str;
	}

}
public class Test {
    public static void main(String[] args) {
		System.out.print("父类信息测试:");
		NonMotor m=new NonMotor("天宇牌","红",4,2);
		System.out.println(m.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());
	}
}


写回答

1回答

好帮手慕小蓝

2022-04-10

同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。

祝学习愉快~


0

0 学习 · 9886 问题

查看课程