2-23编程练习
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 | public class NonMotor { //编写私有属性 private String brand; private String color; private int wheel= 2 ; private int seat= 1 ; //父类无参构造 public NonMotor() { } //父类双参构造 public NonMotor(String color,String brand) { this .setColor(color); this .setBrand(brand); } //父类四参构造 public NonMotor(String brand,String color, int wheel, int seat) { this .setBrand(brand); this .setColor(color); this .setWheel(wheel); this .setSeat(seat); } public String getBrand() { return 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 wheel; } public void setWheel( int wheel) { this .wheel = wheel; } public int getSeat() { return seat; } public void setSeat( int seat) { this .seat = seat; } // 父类方法 描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车. // 其中**的数据由属性提供 public String Work() { String str = "这是一辆" + this .getColor() + "颜色的," + this .getBrand() + "牌的非机动车,有" + this .getWheel() + "个轮子,有" + this .getSeat() + "个座椅的非机动车。" ; return str; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Bicycle extends NonMotor { //自行车子类的无参构造 public Bicycle() { } // 自行车子类的带参构造 public Bicycle(String color,String brand) { //用super();调用父类被允许调用的构造方法(双参构造) super ( "黄" , "捷安特" ); // this.setColor(color); // this.setBrand(brand); } //自行车子类重写父类运行方法 public String Work() { String str= "这是一辆" + this .getColor()+ "颜色的," + this .getBrand()+ "牌的自行车。" ; return str; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class ElectricVehicle extends NonMotor { //新增子类特有"电池品牌"成员属性 private String batteryBrand; //电动车子类无参构造 public ElectricVehicle() { } //电动车子类带参构造 public ElectricVehicle(String batteryBrand) { this .setBatteryBrand(batteryBrand); } public String getBatteryBrand() { return batteryBrand; } public void setBatteryBrand(String batteryBrand) { this .batteryBrand = batteryBrand; } //电动车子类重写父类方法 public String Work() { String str= "这是一辆使用" + this .getBatteryBrand()+ "牌电池的电动车。" ; return str; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class Test { public static void main(String[] args) { // TODO Auto-generated method stub System.out.print( "父类测试信息:" ); NonMotor nm= new NonMotor( "天宇" , "红" , 4 , 2 ); System.out.println(nm.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()); } } |
麻烦老师帮我看下编程练习代码有没有什么问题 哪里需要优化?
1
收起
正在回答
2回答
同学你好,是否忘记粘贴Tricycle三轮车的类了呢~粘贴出来的代码是正确的,
但是要注意一下命名规范, 当方法名由一个单词组成时,则该单词均小写。当由多个单词组成时,第一个单词所有字母均小写,从第二个单词开始,每个单词的首字母大写。如:Work应改为:work。
同学也可以将Tricycle类进行粘贴,老师进行检查一下哦~注意要在“我要回答”中贴出代码,而不要在回复中粘贴代码,会失去格式哦~
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
慕后端4084011
2019-10-11 16:02:30
1 2 3 4 5 6 7 8 9 10 11 12 | public class Tricycle extends NonMotor{ //在三轮车子类无参构造中实现对轮子属性值进行修改 public Tricycle() { this .setWheel( 3 ); } //三轮车子类重写父类方法 public String Work() { String str= "三轮车是一款有" + this .getWheel()+ "个轮子的非机动车" ; return str; } } |
不好意思老师 麻烦再给看看 /笑哭
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧