有没有需要改进的地方
相关截图:
怎么把包名那一串消除掉??
相关代码:
package com.imooc.tese;
//电动车子类
public class Ddcar extends Car{
// 电池 私有属性:battery
private String battery;
//无参构造
public Ddcar() {
}
//get set
public String getBattery() {
return battery;
}
public void setBattery(String battery) {
this.battery = battery;
}
public Ddcar(String battery) {
this.battery=battery;
}
public Car run() {
System.out.println("这是一辆使用"+this.battery+"的电动车");
return new Car();
}
相关代码:
package com.imooc.tese;
//三轮车子类
public class Slcar extends Car{
//无参构造
public Slcar() {
super.setWheel(3);
}
public Car run() {
System.out.println("三轮车是一款有"+this.getWheel()+"轮子的非机动车");
return new Car();
}
}
相关代码:
package com.imooc.tese;
/*
* brand 品牌
* colour 颜色
* wheel 轮子
* chair 座椅
*/
public class Car {
private String brand;
private String colour;
private int wheel=2;
private int chair=1;
//无参构造
public Car() {
System.out.println("");
}
//双参构造
public Car(String brand, String colour) {
this.brand=brand;
this.colour=colour;
}
//四参构造
public Car(String brand,String colour,int wheel,int chair) {
this.brand=brand;
this.colour=colour;
this.wheel=wheel;
this.chair=chair;
}
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 Car run() {
System.out.println("这是一辆"+this.getColour()+"的,"+this.getBrand()+
"的非机动车"+"有"+this.getChair()+"个有"+this.getWheel()+"个轮子");
return new Car();
}
}
相关代码:
package com.imooc.tese;
//自行车子类
public class Zxcar extends Car {
//子类无参构造
public Zxcar() {
}
/*
* brand 品牌
* colour 颜色
*/
public Zxcar(String colour, String brand) {
super(brand, colour);
}
public Car run() {
System.out.println("这是一辆"+this.getColour()+","+this.getBrand()+"的自行车");
return new Car();
}
}
相关代码:
package com.imooc.tese;
public class Test {
public static void main(String[]age) {
System.out.print("父类测试信息:");
Car one=new Car("天宇牌","红色的",4,2);
System.out.println(one.run());
System.out.print("自行车测试信息:");
Zxcar two=new Zxcar("黄色的","捷安特牌");
System.out.println(two.run());
System.out.print("电动车测试信息:");
Ddcar three=new Ddcar("飞鸽牌电池");
System.out.println(three.run());
System.out.print("三轮车测试信息:");
Slcar four=new Slcar();
System.out.println(four.run());
}
}
10
收起
正在回答
1回答
同学你好,在run()方法中不需要创建对象,同学可以返回描述信息。
以Car类和和Ddcar类为例,参考代码如下:
祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9400 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星