【发布时间】:2018-11-17 06:12:47
【问题描述】:
我正在尝试创建一个类来列出有关汽车的信息。我下面的两个文件是 CarClass.Java 和 Car.java。
运行 CarClass.Java 时,我在“Car car1 = new Car(2018, "Black", "Chevy", "Corvette", 250);" 上收到一个错误一行代码。
错误:类 Car 中的构造函数 Car 不能应用于给定类型;
必需:无参数
找到:int,String,String,String,int
原因:实际参数列表和形式参数列表的长度不同
1 个错误
我的问题是我需要进行哪些更改才能修复此错误,以便我的程序能够正常运行?
import java.io.PrintStream;
public class CarClass
{
public static void main(String[] args)
{
Car car1 = new Car(2018, "Black", "Chevy", "Corvette", 250);
System.out.println("The " + car1.getYear() + " " + car1.getColor() + " " + car1.getMake() + " " + car1.getModel() + "Top speed is " + car1.getSpeed() +
" mph.");
}
}
--------------------------------------------------------------------------------
public class Car
{
private int carYear;
private String carColor;
private String carMake;
private String carModel;
private int carSpeed;
public void Car(int year, String color, String make, String model, int speed)
{
this.carYear = year;
this.carColor = color;
this.carMake = make;
this.carModel = model;
this.carSpeed = speed;
}
public int getYear()
{
return this.carYear;
}
public String getColor()
{
return this.carColor;
}
public String getMake()
{
return this.carMake;
}
public String getModel()
{
return this.carModel;
}
public int getSpeed()
{
return this.carSpeed;
}
}
【问题讨论】:
标签: java class constructor