基本问题,我已尝试应用其他几个相关问题来解决这个问题,但这总是回归为空。
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();
}
}
答案 0 :(得分:2)
您的Consultant
类有一个构造函数,它将Name
作为参数。
实例化Consultant
时,您必须传入Name
firstName
,middleName
,lastName
为空的Consultant
对象。
我建议您在新建Name
对象之前在一行上标记调试器并检查传入的Name.firstName
对象,看看Consultant
是否有任何值,等
我还会在firstName
的构造函数中添加断点,以观察middleName
,lastName
,Name
的值。
<强>更新强>
您刚刚发布了public Name(String lastName, String firstName){
}
的代码,您的构造函数什么也没做。您的代码为:
public Name(String lastName, String firstName){
this.lastName = lastName;
this.firstName = firstName;
}
应该阅读
{{1}}
答案 1 :(得分:0)
您将名称初始化为:
Name contact = new Name(lName, fName);
但是你没有做任何事情
public Name(String lastName, String firstName){
}
并在您的Name
课程中
private String firstName;
private String middleName;
private String lastName;
全部初始化为null
s。所以当你打电话时
public void setFirstName(){
firstName = name.getFirstName();
}
public void setMiddleName(){
middleName = name.getMiddleName();
}
public void setLastName(){
lastName = name.getLastName();
}
在Consultant
课程中,他们获得全部null
。