源代码
public class Commodity_information{
public static void main(String args[]){
commodities_son commodity1=new commodities_son(101,"商品实例1",19.9,"某生产商");
System.out.println("商品实例1信息:\n"+commodity1.toString());
commodity1.revise_price(29.9);
System.out.println("修改价格后:\n"+commodity1.toString());
}
}
abstract class commodities{
int number;
String name;
double price;
String manufacturer;
public commodities(){
}
public commodities(int number,String name,double price,String manufacturer){
this.number=number;
this.price=price;
this.name=name;
this.manufacturer=manufacturer;
}
public void revise_price(double price){
this.price=price;
}
abstract public String toString();
}
class commodities_son extends commodities{
public String toString(){
return number+" "+name+" "+price+" "+manufacturer;
}
}
这样会出现下面的报错:
Commodity_information.java:3: 错误: 无法将类 commodities_son中的构造器 commodities_son应用到给定类型;
commodities_son commodity1=new commodities_son(101,"商品实例1",19.9,"某生产商");
^
需要: 没有参数
找到: int,String,double,String
原因: 实际参数列表和形式参数列表长度不同
1 个错误
工具以退出代码 1 完成
是不是commodities_son子类是不是不能调用父类的构造方法?
我自己如果给子类添加子类自己的构造方法就不会报错了
class commodities_son extends commodities{
public commodities_son(int number,String name,double price,String manufacturer){
super(number,name,price,manufacturer);
}
public String toString(){
return number+" "+name+" "+price+" "+manufacturer;
}
}
我看到其他帖子说子类肯定会调用父类的构造方法,很困惑,求解报错原因和问题