0

基本问题,我尝试应用其他几个相关问题来解决这个问题,但这总是返回为空。

public class Consultant {

public Name name;
String lastName;
String firstName;
String middleName;

public Consultant (Name name){
    this.name = name;
    this.lastName = name.getLastName();
    this.firstName = name.getFirstName();
    this.middleName = name.getMiddleName();
}

public Name getName(){
    return name;
}

public void setFirstName(){
    firstName = name.getFirstName();
}

public void setMiddleName(){
    middleName = name.getMiddleName();
}

public void setLastName(){
    lastName = name.getLastName();
}

@Override
public String toString(){
    return lastName + ", " + firstName + " " + middleName;
}

}

为什么我尝试在任何地方使用它作为打印输出,我看到的只是 null,null null。我已经尝试多次调整代码无济于事。

这是成功运行的测试类片段,只返回名称的空值:

public void consultantTimeToStringTest() {
    String fName = "Test";
    String lName = "Dude";
    Name contact = new Name(lName, fName);
    Consultant testConsultant = new Consultant(contact);
    System.out.println(testConsultant.toString());

这里也是 Name 类:

public class Name {

private String firstName;
private String middleName;
private String lastName;

public Name(){

}

public Name(String lastName, String firstName){

}

public Name(String lastName, String firstName, String middleName){
    this.firstName = firstName;
    this.middleName = middleName;
    this.lastName = lastName;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Name other = (Name) obj;
    if (firstName == null) {
        if (other.firstName != null)
            return false;
    } else if (!firstName.equals(other.firstName))
        return false;
    if (lastName == null) {
        if (other.lastName != null)
            return false;
    } else if (!lastName.equals(other.lastName))
        return false;
    if (middleName == null) {
        if (other.middleName != null)
            return false;
    } else if (!middleName.equals(other.middleName))
        return false;
    return true;
}

public String getFirstName(){
    return firstName;
}

public String getLastName(){
    return lastName;
}

public String getMiddleName(){
    return middleName;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((firstName == null) ? 0 : firstName.hashCode());
    result = prime * result
            + ((lastName == null) ? 0 : lastName.hashCode());
    result = prime * result
            + ((middleName == null) ? 0 : middleName.hashCode());
    return result;
}

public void setFirstName(String firstName){

}

public void setLastName(String lastName){

}

public void setMiddleName(String middleName){

}

public String toString(){
    StringBuilder result = new StringBuilder();
    String NEW_LINE = System.getProperty("line.separator");
    result.append(lastName);
    return result.toString();
}

}

4

2 回答 2

2

您的Consultant类有一个Name作为参数接收的构造函数。

当您实例化 时Consultant,您必须传入一个对, ,Name具有 null 的对象。firstNamemiddleNamelastName

我建议在新建Consultant对象之前在一行上标记调试器并检查Name传入的对象并查看是否有任何值Name.firstName等。

我还会在 的构造函数中添加一个断点来观察, ,Consultant的值是否被设置。firstNamemiddleNamelastName

更新

您刚刚发布了代码,Name而您的构造函数什么也没做。您的代码如下:

public Name(String lastName, String firstName){

}

它应该读

public Name(String lastName, String firstName){
    this.lastName = lastName;
    this.firstName = firstName;
}
于 2013-01-31T03:13:39.553 回答
0

您将名称初始化为:

Name contact = new Name(lName, fName);

但你什么都没做

public Name(String lastName, String firstName){

}

在你的Name课堂上,

private String firstName;
private String middleName;
private String lastName;

都初始化为nulls。所以当你打电话

public void setFirstName(){
    firstName = name.getFirstName();
}

public void setMiddleName(){
    middleName = name.getMiddleName();
}

public void setLastName(){
    lastName = name.getLastName();
}

在你的Consultant课堂上,他们得到了所有null

于 2013-01-31T03:17:07.820 回答