这段代码,super调用参数语法有什么错误呢
来源:2-21 编程练习
weixin_慕的地0390393
2021-10-12 16:56:28
public class Test {
public static void main(String[] args) {
NonMotor one = new NonMotor("天宇","红色",4,2);
System.out.print("父类信息测试:"+one.work()+"\n");
Bicycle two = new Bicycle("捷安特","黄色");
System.out.print("自行车类信息测试:"+tow.work()+"\n");
ElectricVehicle three = new ElectricVehicle("飞鸽");
System.out.print("电动车类信息测试:"+three.work()+"\n");
Triycle four = new Triycle(3);
System.out.print("三轮车类信息测试:"+four.work());
}
}
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String broad;
private String clore;
private int lunzi = 2;
private int chare = 1;
// 无参构造方法
public NonMotor(){
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String broad, String clore){
this.setBroad(broad);
this.setClore(clore);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String broad, String clore, int lunzi, int chare){
this.setBroad(broad);
this.setClore(clore);
this.setLunzi(lunzi);
this.setChare(chare);
}
// 公有的get***/set***方法完成属性封装
public String getBroad(){
return this.broad;
}
public void setBroad(String broad){
this.broad = broad;
}
public String getClore(){
return this.clore;
}
public void setClore(String clore){
this.clore = clore;
}
public int getLunzi(){
return this.lunzi;
}
public void setLunzi(int lunzi){
this.lunzi = lunzi;
}
public int getChare(){
return this.chare;
}
public void setChare(int chare){
this.chare = chare;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str;
str = "这是一辆"+this.getClore()+"颜色的,"+this.getBroad()+"牌的非机动车,有"+this.getLunzi()+"个轮子,有"+this.getChare()+"个座椅的非机动车";
return str;
}
}
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle(){
super(String broad, String clore);
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str;
str = "这是一辆"+this.getClore()+"颜色的,"+this.getBroad()+"牌的自行车";
return str;
}
}
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String dianChiBroad;
// 公有的get***/set***方法完成属性封装
public ElectricVehicle(String dianChiBroad){
this.setDianChiBroad(dianChiBroad);
}
public String getDianChiBroad(){
return this.dianChibroad;
}
public void setDianChiBroad(String dianChiBroad){
this.dianChiBroad = dianChiBroad;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work(){
String str;
str="这是一辆用"+this.getDianChiBroad()+"牌电池的电动车.";
return str;
}
}
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle(){
super();
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str;
str = "三轮车是一款有"+this.getLunzi()+"个轮子的非机动车";
return str;
}
}
2回答
好帮手慕小脸
2021-10-12
同学你好,上述贴出代码存在如下几个问题:
1、super()方法中只传入参数名即可,具体的参数类型不需要写
2、参数要写在方法名的小括号里,若不定义无法使用

3、属性名为two,调用方法时单词书写错误

4、三轮车类的类名称单词书写错误,同学需要注意哟~


5、电动车类中get方法返回电池品牌,直接进行return即可,无需添加this

祝学习愉快~
好帮手慕小小
2021-10-12
同学你好,super()是用来调用父类构造方法,括号内需要直接传入对应的参数而不是再次声明参数类型。
参考代码如下:

祝学习愉快~
相似问题