错误:构造函数 Family(String, String, int) 未定义

huangapple go评论54阅读模式
英文:

Error The constructor Family(String, String, int) is undefined

问题

以下是翻译好的内容:

我对Java还相对新手。我遇到了一个错误:“构造函数Family(String,String,int)未定义”。我不确定这是什么意思。在这里需要一点帮助。

编辑:我漏掉了额外的3个参数,还漏掉了31周围的引号。

Main.java

public class Main {
    
    public static void main(String[] args){
        Family person = new Family("CHRIS", "PEREZ", 31);
        String person1 = person.getPerson();
        System.out.println(person1);    
    }

}

Family.java

public class Family {
    
    String firstName;
    String lastName;
    int age;
    int phoneNumber;
    String dob;
    String married;
    
    public Family(String firstName, String lastName, int age, int phoneNumber,
            String dob, String married) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.phoneNumber = phoneNumber;
        this.dob = dob;
        this.married = married;
    }
    
    public String getPerson() {
        return ("嗨,我叫" + this.firstName + " " + this.lastName + "。" + "我今年" + this.age + "岁。");
    }
    
}
英文:

I am fairly new to Java. I am getting an error "The constructor Family(String, String, int) is undefined". Im not sure what this means. Need a bit of help here please.

EDIT: I was missing the extra 3 parameters and also was missing quotes around 31.

Main.java

public class Main {


public static void main(String[] args){
    Family person = new Family("CHRIS", "PEREZ", 31);
    String person1 = person.getPerson();
	System.out.println(person1);	
}

}

Family.java

public class Family {

String firstName;
String lastName;
int age;
int phoneNumber;
String dob;
String married;

public Family(String firstName, String lastName, int age, int phoneNumber,
		String dob, String married) {
	this.firstName = firstName;
	this.lastName = lastName;
	this.age = age;
	this.phoneNumber = phoneNumber;
	this.dob = dob;
	this.married =  married;
	

public String getPerson() {
	return ("Hi my name is"+this.firstName+" "+ this.lastName+"."+"I am "+this.age+" years old.");
}

}

答案1

得分: 2

这是因为您的Family类只有一个需要提供所有六个字段的六参数构造函数。您的调用:

Family person = new Family("CHRIS", "PEREZ", 31);

只提供了所需的六个字段中的三个。您可以重写构造函数,例如:

public Family(String firstName, String lastName, int age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    ...
}

但您应该处理您在这里没有提供但在其他构造函数中提供的其余字段。

英文:

This is because your Family class only has a six-argument constructor requiring all of the six fields to be provided. Your call:

Family person = new Family("CHRIS", "PEREZ", 31);

only provides three of the six required. You could override the constructor, for instance:

public Family(String firstName, String lastName, int age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    ...
}

but you should do something with the rest of the fields that you didn't provide here that you provide in the other constructor.

答案2

得分: 1

你正在这样调用 Family 类的构造函数:

Family person = new Family("CHRIS", "PEREZ", 31);

但是类中唯一的构造函数是这样定义的:

public Family(String firstName, String lastName, int age, int phoneNumber,
    String dob, String married) {

注意它有比你传入的参数更多:phoneNumber, dob, married。在Java中,你必须为所有参数提供值:

Family person = new Family("CHRIS", "PEREZ", 31, 123456, "5/Apr/1975", "who's asking");

或者,你需要定义一个只需要 firstName, lastNameage 的新构造函数。

英文:

You're calling the Family class constructor like this:

Family person = new Family("CHRIS", "PEREZ", 31);

But the only constructor in the class is defined like this:

public Family(String firstName, String lastName, int age, int phoneNumber,
    String dob, String married) {

See that it has more parameters than you're passing in: phoneNumber, dob, married. In Java you have to give values to all parameters:

Family person = new Family("CHRIS", "PEREZ", 31, 123456, "5/Apr/1975", "who's asking");

Or, you need to define a new constructor that needs only firstName, lastName and age.

答案3

得分: 0

这也可能发生在以您指定的顺序调用构造函数参数不正确的情况下。

英文:

This can also happen when calling the constructor arguments in the wrong order that you specified them in.

huangapple
  • 本文由 发表于 2020年4月9日 05:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61110033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定