m_7_maranda 2017-10-29 02:26 采纳率: 50%
浏览 696

调用父类构造函数super(color,wheel)出错

class Vehicle
{
private String color;
private int wheel;
public Vehicle()
{
System.out.println("调用了Vehicle的无参构造函数");
}
public Vehicle(String color,int wheel)
{
System.out.println("调用了Vehicle的有参构造函数");
this.color=color;
this.wheel=wheel;
}
public void show()
{
System.out.println("颜色为:"+color+" 车轮数量为:"+wheel);
}
public void calc()
{
float velocity=0;
float distance=0;
float t=velocity/distance;
System.out.println("速度为:"+velocity+" 距离为:"+distance+" 行使该距离所需的时间为:"+t);
}
}

class Car extends Vehicle
{
private String co;
private int wh;
public Car()
{
System.out.println("调用了Car的无参构造函数");
}
public Car(String color,int wheel,String c,int w)
{
System.out.println("调用了Car的有参构造函数");
super(color,wheel);
co=c;wh=w;
System.out.println("小车颜色为:"+color+",车轮数量为:"+wheel);
}
public void consume()
{
double oil=0;
float distance=0;
double l=oil*distance;
System.out.println("百公里耗油量为:"+oil+",距离为:"+distance+",行使该距离所耗的油量为:"+l);
}
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-10-29 03:03
    关注

    super调用必须放在构造函数的第一行。

    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python