老师您看看我写的对不对啊

来源:3-1 总结

夏蔚海

2020-01-11 23:31:47

package Bclx;

public class NonMotor {
   // 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
   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);
   }
   // 公有的get***/set***方法完成属性封装
   //品牌

   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 = "这是一辆" + this.getColour() + "颜色的," + this.getBrand() + "牌的非机动车,有" +
               this.getWheel() + "个轮子,有" + getChair() + "个座椅的非机动车。";
       return str;
   }
}
package Bclx;

public class Bicycle  extends NonMotor{
   // 在构造方法中调用父类多参构造,完成属性赋值
   public Bicycle(){

   }
   public Bicycle(String brand,String colour){
       super(brand, colour);
//        this.setBrand(brand);
//        this.setColour(colour);
   }
   // 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
   public String work() {
       String str = "这是一辆" + this.getColour() + "颜色的," + this.getBrand() + "牌的自行车。";
       return str;
   }
}
package Bclx;

public class ElectricVehicle extends NonMotor {
   // 私有属性:电池品牌
   private String batteryBrand;
   //无参构造
   public ElectricVehicle(){

   }
   //带参构造
   public ElectricVehicle(String batteryBrand){
       this.setBatteryBrand(batteryBrand);
   }
   // 公有的get***/set***方法完成属性封装

   public String getBatteryBrand() {
       return batteryBrand;
   }

   public void setBatteryBrand(String batteryBrand) {
       this.batteryBrand = batteryBrand;
   }

   // 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
   public String work() {
       String str = "这是一辆" + this.getBatteryBrand() + "牌电池的电动车";
       return str;
   }
}
package Bclx;

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

import Bclx.Bicycle;
import Bclx.ElectricVehicle;
import Bclx.NonMotor;
import Bclx.Tricycle;

public class test {
   public static void main(String[] args) {
       NonMotor non = new NonMotor("红色","天宇",4,2);
       Bicycle bic = new Bicycle("小鸟","黑色");
       ElectricVehicle ele = new ElectricVehicle("英国");
       Tricycle tri = new Tricycle();
       System.out.println("父类信息测试:" + non.work());
       System.out.println("自行车类信息测试:" + bic.work());
       System.out.println("电动车类信息测试:" + ele.work());
       System.out.println("三轮车类信息测试:" + tri.work());
   }
}

写回答

1回答

好帮手慕小尤

2020-01-12

同学你好,程序正确,但有一个小建议,当类名由一个单词组成时,该单词首字母大写;如果类名由多个单词组成,则每个单词的首字母均大写。如:test应改为:Test

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

0

0 学习 · 11489 问题

查看课程