我奇怪地遇到了一个为我的联系人类打印 null 的问题。它似乎不喜欢 PhoneNumberList 的数字构造函数。我只是想返回跟踪数字等的计数,但它只为 getNumber 和 getNumberCount 返回 null。
尝试将计数从私有更改为公共以直接访问以及更改名称和构造函数。
public class Contact{
private String name;
private PhoneNumberList numbers;
// Purpose:
// initialize this instance of Contact
// with no PhoneNumber
//
public Contact (String theName)
{
// You must allocate a PhoneNumberList here
numbers = new PhoneNumberList();
name = theName;
}
// Purpose:
// initialize this instance of Contact
// add p to the list of phone numbers associated with
// this Contact
//
public Contact (String theName, PhoneNumber p)
{
// You must allocate a PhoneNumberList here
PhoneNumberList numbers = new PhoneNumberList();
name = theName;
numbers.add(p);
}
// Purpose:
// return the name associated with this instance
//
public String getName ()
{
return name;
}
// Purpose:
// change the name associated with this instance to be newName
//
public void setName(String newName)
{
name = newName;
}
// Purpose:
// add a new PhoneNumber to this contact
// there is no maximum number of phone numbers that can be
// assigned to a contact.
//
public void addNumber (PhoneNumber p)
{
numbers.add(p);
}
// Purpose:
// remove p from the list of PhoneNumbers associated with this contact
// if p is not in the list, do nothing.
//
public void removeNumber (PhoneNumber p)
{
int index = numbers.find(p);
numbers.remove(index);
}
// Purpose:
// return the count of PhoneNumbers associated with this contact
//
public int getNumberCount()
{
return numbers.count;
}
红糖糍粑
相关分类