创建子类构造函数时发生Java错误
我试图学习一些Java,但我在创建子类时遇到了困难。我不断得到一个创建子类构造函数时发生Java错误,java,inheritance,Java,Inheritance,我试图学习一些Java,但我在创建子类时遇到了困难。我不断得到一个错误,在…中没有可用的默认构造函数 代码如下: class Car { String name; int speed; int gear; int drivetrain; String direction; String color; String fuel; public Car(String carName, int carSpeed, String carD
错误,在…
中没有可用的默认构造函数
代码如下:
class Car
{
String name;
int speed;
int gear;
int drivetrain;
String direction;
String color;
String fuel;
public Car(String carName, int carSpeed, String carDirection, String carColor, int carDriveTrain, int carGear)
{
name = carName;
speed = carSpeed;
gear = carGear;
drivetrain = carDriveTrain;
direction = carDirection;
color = carColor;
fuel = "Gas";
}
void shiftGears(int newGear){gear = newGear; }
void accelerateSpeed(int acceleration){speed = speed + acceleration; }
void applyBrake(int brakingFactor){ speed = speed - brakingFactor;}
void turnWheel(String newDirection){ direction = newDirection; }
}//end of Car class
class Suv extends Car
{
void applyBrake(int brakingFactor)
{
super.applyBrake(brakingFactor);
speed = speed - brakingFactor;
}
}
当我尝试创建“Suv”子类时,问题就出现了。我做错了什么?谢谢 将以下构造函数添加到
Car
:
public Car(){}
问题是无法创建Suv
,因为要运行Suv
的默认构造函数,需要首先运行Car
的构造函数,而Car
只有一个接受参数的构造函数,因此不能将其用作默认构造函数
另一种方法是,正如@Markus所建议的,在
Suv
中实现一个构造函数,该构造函数将使用所有必需的参数调用super
。无论哪种方式,其主要思想是为了实例化Suv
,我们需要能够首先实例化Car
,否则我们将得到一个编译器错误。将以下构造函数添加到Car
:
public Car(){}
问题是无法创建Suv
,因为要运行Suv
的默认构造函数,需要首先运行Car
的构造函数,而Car
只有一个接受参数的构造函数,因此不能将其用作默认构造函数
另一种方法是,正如@Markus所建议的,在Suv
中实现一个构造函数,该构造函数将使用所有必需的参数调用super
。无论哪种方式,其主要思想是为了实例化Suv
,我们需要能够首先实例化Car
,否则我们将得到一个编译器错误。因为“Car”有一个构造函数,所以从Car派生的任何子类也需要一个构造函数。因此,首先需要在Suv类中放置一个构造函数
例如:
class Suv extends Car {
public Suv() {
super( /* here you need to pass arguments to create a car */);
// any other constructor code
}
}
因为“Car”有一个构造函数,所以从Car派生的任何子类也需要一个构造函数。因此,首先需要在Suv类中放置一个构造函数
例如:
class Suv extends Car {
public Suv() {
super( /* here you need to pass arguments to create a car */);
// any other constructor code
}
}
您可能希望在
Suv
中创建以下构造函数,以初始化Car
构造函数具有的参数:
public Suv(String carName, int carSpeed, String carDirection, String carColor, int carDriveTrain, int carGear)
{
super (carName, carSpeed, carDirection, carColor, carDriveTrain,carGear);
}
另一种方法是将无参数构造函数添加到Car
,在这种情况下,Sub
的默认构造函数将调用该构造函数:
public Car()
{
}
您可能希望在
Suv
中创建以下构造函数,以初始化Car
构造函数具有的参数:
public Suv(String carName, int carSpeed, String carDirection, String carColor, int carDriveTrain, int carGear)
{
super (carName, carSpeed, carDirection, carColor, carDriveTrain,carGear);
}
另一种方法是将无参数构造函数添加到Car
,在这种情况下,Sub
的默认构造函数将调用该构造函数:
public Car()
{
}
我建议不要将SUV类放在同一个文件中。相反,在当前包中创建另一个类,将其命名为SUV,对其进行扩展,并通过以下两种语法之一调用超类构造函数:
super(); //calls the superclass no-argument constructor with no parameter list
或
我建议不要将SUV类放在同一个文件中。相反,在当前包中创建另一个类,将其命名为SUV,对其进行扩展,并通过以下两种语法之一调用超类构造函数:
super(); //calls the superclass no-argument constructor with no parameter list
或
我认为在
Suv
中需要一个构造函数,它可以获得与Car
相同的参数。长话短说,Java中的类不是这样工作的。你不需要“super”。你必须声明你的方法受保护。没有公共/私有/受保护的标识符会使它们仅在包级别可见。@mlcim您所说的大部分都是不正确的。每个文件可以有多个类。子类不必“声明为静态”。方法隐私与问题中的任何内容无关。@khelwood是对的-值得您进一步评论:每个文件只能有一个公共类,但可以有多个非公共类。我认为您需要在Suv
中使用一个构造函数,该构造函数的参数与Car
相同。长话短说,Java中的类不是这样工作的。你不需要“超级”。你必须声明你的方法是受保护的。没有公共/私有/受保护的标识符会使它们仅在包级别可见。@mlcim您所说的大部分都是不正确的。每个文件可以有多个类。子类不必“声明为静态”。方法隐私与问题中的任何内容无关。@khelwood是对的,值得您多加评论:每个文件只能有一个公共类,但可以有多个非公共类。