简体   繁体   中英

I need to give 2 objects as a parameter in a constructor, but I'm struggling to find the right way to do it

public class Phone{

public String kind;
public String number;

    public Phone(String kind, String number) {
        this.kind = kind;
        this.number = number;
    }
}

public class ContactData{

public String eMail;

    Phone phone = new Phone("phone", "031234567");
    Phone cellphone = new Phone("cellphone", "0499209802");

    public ContactData(String eMail, Phone phone, Phone cellphone){
        this.eMail = eMail;
        this.phone = phone;
        this.cellphone = cellphone;
    }
}

我已经尝试了很多东西,但是我一直收到这个错误

There is a type mismatch between constructor parameters and what you are actually passing:

public ContactData(String eMail, Phone phone, Phone cellphone)

However you are using a String object instead of a Phone :

new Contact("ab@abc.com","123","123");

What you really need to pass is an instance of Phone :

new Contact("ab@abc.com",new Phone("phone", "031234567"),
            new Phone("phone", "031234567"));

In your picture you are trying to declare a ContactData instance using a constructor with (String, String, String) parameters, but the only constructor you have takes a (String, Phone, Phone).

You can fix this by creating Phone instances when declaring the ContactData instance directly, like this:

ContactData contactData1 = new ContactData("nelson@hotmail.be", new Phone("cellphone", "154789562"), new Phone("cellphone", "1234567890"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM