2-23代码检查及疑问
Test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Test { public static void main(String[] args) { System.out.print( "父类信息测试:" ); NonMotor a = new NonMotor( "红" , "天宇" , 4 , 2 ); System.out.println(a.work()); System.out.print( "自行车类信息测试:" ); Bicycle b = new Bicycle( "黄" , "捷安特" ); System.out.println(b.work()); System.out.print( "电动车类信息测试:" ); ElectricVehicle c = new ElectricVehicle( "飞鸽" ); System.out.println(c.work()); System.out.print( "三轮车类信息测试:" ); Tricycle d = new Tricycle(); System.out.println(d.work()); } } |
NonMotor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | public class NonMotor { // 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个) private String name; private String color; private int wheelNum = 2 ; private int seatNum = 1 ; // 无参构造方法 public NonMotor() { } // 双参构造方法,完成对品牌和颜色的赋值 public NonMotor(String name,String color) { this .setName(name); this .setColor(color); } // 四参构造方法,分别对所有属性赋值 public NonMotor(String name, String color, int wheelNum, int seatNum) { this .setName(name); this .setColor(color); this .setWheelNum(wheelNum); this .setSeatNum(seatNum); } // 公有的get***/set***方法完成属性封装 public String getName() { return name; } public void setName(String name) { this .name = name; } public String getColor() { return color; } public void setColor(String color) { this .color = color; } public int getWheelNum() { return wheelNum; } public void setWheelNum( int wheelNum) { this .wheelNum = wheelNum; } public int getSeatNum() { return seatNum; } public void setSeatNum( int seatNum) { this .seatNum = seatNum; } // 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供 public String work() { String str = "这是一辆" + this .getColor() + "颜色的," + this .getName() + "牌的非机动车,有" + getWheelNum() + "个轮子,有" + getSeatNum() + "个座椅。" ; return str; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Bicycle extends NonMotor{ // 在构造方法中调用父类多参构造,完成属性赋值 public Bicycle(String name,String color) { super (name,color); } // 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供 public String work() { String str = "这是一辆" + this .getColor() + "颜色的," + this .getName() + "牌的自行车。" ; return str; } } |
ElectricVehicle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class ElectricVehicle extends NonMotor { // 私有属性:电池品牌 private String batteryName; // 公有的get***/set***方法完成属性封装 public ElectricVehicle(String batteryName) { this .setBatteryName(batteryName); } public String getBatteryName() { return batteryName; } public void setBatteryName(String batteryName) { this .batteryName = batteryName; } // 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供 public String work() { String str = "这是一辆使用" + this .getBatteryName() + "牌电池的电动车。" ; return str; } } |
Tricycle
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Tricycle extends NonMotor { // 在无参构造中实现对轮子属性值进行修改 public Tricycle() { super (); setWheelNum( 3 ); } // 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供 public String work() { String str = "三轮车是一款有" + this .getWheelNum() + "个轮子的非机动车。" ; return str; } } |
子类实例化时会默认调用父类无参构造函数,那么三轮车类的无参构造是不是不需要加super了?
0
收起
正在回答
2回答
同学你好,
子类调用父类无参构造方法时,可以省略super();哦~
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧