类中的 java 构造函数不能应用于给定类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23889811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java constructor in class cannot be applied to given types
提问by user3679454
I have 2 subclasses: Staff, Student they belong to superclass Person.
我有 2 个子类:Staff、Student,它们属于超类 Person。
Here is the code(tasks) which is given by my teacher:
这是我老师给的代码(任务):
public class Person
{
private String name;
private int yearOfBirth;
/**
* Create a person with given name and age.
*/
Person(String name, int yearOfBirth)
{
this.name = name;
this.yearOfBirth = yearOfBirth;
}
}
class Student extends Person
{
private String SID; // student ID number
/**
* Create a student with no parameters.
*/
Student()
{
//task.
}
}
public class Staff extends Person
{
private String roomNumber;
/**
* Construct a staff member with field values and no pamaeters.
*/
public Staff()
{
//task
}
}
I don't know what can I type in order to create an object without parameter. It always appears an error like: constructor Person in class Person cannot be applied to given types; required: java.lang.String,int;
我不知道我可以输入什么来创建没有参数的对象。它总是出现这样的错误:类 Person 中的构造函数 Person 不能应用于给定类型;要求:java.lang.String,int;
I have checked online that there are 2 ways to solve the problem:
我在网上查了一下,有两种方法可以解决这个问题:
add a default value in the superclass:
Person()//without parameter
.In the subclass Student:
在超类中添加一个默认值:
Person()//without parameter
.在子类学生中:
Student()
{
Person astudent = new Student() //I guess.
}
- add a super() in the subclass:
- 在子类中添加一个 super() :
Student()
{
super("xxx")//I guess.
}
I don't know what to do. I an a starter in learning BlueJ. Hope anyone can help me. Thank you very much.
我不知道该怎么办。我是学习 BlueJ 的初学者。希望任何人都可以帮助我。非常感谢。
采纳答案by Abimaran Kugathasan
Since your super class Person
doesn't have a default constructor, in your sub classes (Student
and Staff
), you must call the super class constructor as the first statement.
由于您的超类Person
没有默认构造函数,因此在您的子类(Student
和Staff
)中,您必须调用超类构造函数作为第一条语句。
You should define your sub class constructors like this:
你应该像这样定义你的子类构造函数:
Student() {
super("a_string_value", an_int_value);// You have to pass String and int values to super class
}
Staff() {
super("a_string_value", an_int_value); // You have to pass String and int values to super class
}
回答by Stultuske
the first thing a constructor will do, is call the constructor (with same arguments) of the super class. Person does not have a no-argument constructor, so, you must change your code in one of next two ways:
构造函数要做的第一件事是调用超类的构造函数(具有相同的参数)。Person 没有无参数构造函数,因此,您必须通过以下两种方式之一更改代码:
Student(String name, int yearOfBirth)
{
//task.
}
or
或者
Student()
{
super("", 0);
//task.
}
and the same goes for Staff
员工也是如此
回答by Keerthivasan
Add super(NAME_IN_STRING_TYPE,YEAR_OF_BIRTH_IN_INT_TYPE);
as a first statement in your subclasse's constructor like
super(NAME_IN_STRING_TYPE,YEAR_OF_BIRTH_IN_INT_TYPE);
在子类的构造函数中添加为第一条语句,例如
Student constructor
学生构造函数
Student()
{
super("name", 1970); // String,int arguments passed
//task.
}
Staff constructor
员工建设者
Staff()
{
super("name", 1970); // String,int arguments passed
//task.
}
This is needed since there is no default no-arg constructor in the base class. You have to explicitly define a no-arg constructor in base class or you need to instruct the compiler to call the custom constructor of the base class.
这是必需的,因为基类中没有默认的无参数构造函数。您必须在基类中显式定义无参数构造函数,或者您需要指示编译器调用基类的自定义构造函数。
Note : Compiler will not add default no-arg constructor in a class if it has a user defined constructor. It will add the default no-arg constructor only when there is no constructor defined in the class.
注意:如果类具有用户定义的构造函数,则编译器不会在类中添加默认的无参数构造函数。只有当类中没有定义构造函数时,它才会添加默认的无参数构造函数。
回答by copakawusau
Try this:
尝试这个:
Student(String name, int yearOfBirth) {
super(name, yearOfBirth);
// task...
}
Reason: you dont have a default constructor at your superclass. So you have to call super()
at the first position in your subclass constructor.
原因:您的超类没有默认构造函数。所以你必须super()
在你的子类构造函数的第一个位置调用。
回答by Macrosoft-Dev
for constructor no param you should have two constructors like
对于没有参数的构造函数,您应该有两个构造函数,例如
public class Student {
Student(String name , int dateOfBirth)
{
super(name,dateOfBirth)
}
Student()
{
//task.
}
}
also same for other class
其他班级也一样
回答by Alexey Malev
To construct instance of Student
you need to do actions neccesary to construct Person
first. There is only one way to construct Person
- two-arg constructor. That means you have to change Student
like:
要构建实例,Student
您需要先执行必要的操作才能构建Person
。只有一种构造方法Person
- 两个参数的构造函数。这意味着你必须Student
像这样改变:
public Student() {
super("someName", 1950); //first values came to my mind
}
Although you should be aware that Student
should behave exactly like Person
if treated as Person
, i.e. have age and name. So actually I'd recommend to change Student
constructor to include name and age there.
虽然您应该知道,Student
应该表现得与Person
if 处理为完全一样Person
,即有年龄和姓名。所以实际上我建议更改Student
构造函数以在其中包含名称和年龄。