基本问题,我尝试应用其他几个相关问题来解决这个问题,但这总是返回为空。
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();
}
}