运行不了,求解答
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 | package com.bike; public class NonMotor { // 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个) private String brand; private String color; private int wheel; private int seat; // 无参构造方法 public NonMotor(){ } // 双参构造方法,完成对品牌和颜色的赋值 public NonMotor(String brand,String color){ this .brand=brand; this .color=color; } // 四参构造方法,分别对所有属性赋值 public NonMotor(String brand,String color, int wheel, int seat) { this .brand=brand; this .color=color; this .wheel=wheel; this .seat=seat; } // 公有的get***/set***方法完成属性封装 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() { System.out.println( "这是一辆" +color+ "颜色的," +brand+ "的非机动车," + "有" +wheel+ "个轮子," + "有" +seat+ "个座椅的非机动车。" ); return str; } } |
1
收起
正在回答
6回答
同学你好,在实现类的work()方法中,定义为有返回值的方法,返回值类型为String类型,所以在方法中应该定义String类型的变量str,然后返回str、例如:
1 2 3 4 | public String work() { String str = "这是一辆" +getColor()+ "颜色的," +getBrand()+ "的自行车," ; return str; } |
其他实现类中的work()方法参考如上。在Test类中,需要使用System.out.println(wd1.work());将其输出。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
codeJeffry
2019-04-05 21:21:51
codeJeffry
2019-04-05 21:19:32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.bike; public class Test { public static void main(String[] args) { System.out.print( "父类信息测试:" ); NonMotor wd= new NonMotor(); wd.setColor( "红色" ); wd.setBrand( "天宇牌" ); wd.setWheel( 4 ); wd.setSeat( 2 ); wd.work(); System.out.print( "自行车类信息测试:" ); Bicycle wd1= new Bicycle( "黄色" , "捷安特" ); wd1.work(); System.out.print( "电动车类信息测试:" ); ElectricVehicle wd2= new ElectricVehicle(); wd2.setElectricBrand( "飞鸽" ); wd2.work(); System.out.print( "三轮车类信息测试:" ); Tricycle wd3= new Tricycle(); wd3.work(); } } |
codeJeffry
2019-04-05 21:19:06
1 2 3 4 5 6 7 8 9 10 11 12 | package com.bike; public class Tricycle extends NonMotor { // 在无参构造中实现对轮子属性值进行修改 public Tricycle() { super .setWheel( 3 ); } // 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供 public String work() { System.out.println( "三轮车是一款有" +getWheel()+ "个轮子的非机动车。" ); return work(); } } |
codeJeffry
2019-04-05 21:18:27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.bike; public class ElectricVehicle extends NonMotor{ // 私有属性:电池品牌 private String electricBrand; // 公有的get***/set***方法完成属性封装 public void setElectricBrand(String electricBrand) { this .electricBrand=electricBrand; } public String getElectricBrand() { return electricBrand; } // 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供 public String work() { System.out.println( "这是一辆使用" +electricBrand+ "牌电池的电动车。" ); return work(); } } |
codeJeffry
2019-04-05 21:17:47
1 2 3 4 5 6 7 8 9 10 11 12 | package com.bike; public class Bicycle extends NonMotor{ // 在构造方法中调用父类多参构造,完成属性赋值 public Bicycle(String brand,String color) { super (brand,color); } // 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供 public String work() { System.out.println( "这是一辆" +getColor()+ "颜色的," +getBrand()+ "的自行车," ); return work(); } } |
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧