gpt4 book ai didi

java - 对象返回 null

转载 作者:行者123 更新时间:2023-12-03 20:46:47 25 4
gpt4 key购买 nike

总结:Java 新手,尝试查看其他帖子但没有找到答案。我正在学习继承,并且有一个由 Runner 类扩展的 AddressBook 类。当我编写程序来测试继承时,我创建了一个 Runner 对象。如果我得到第一个 String 参数,它返回正常,但是当我尝试获取第二个 String 参数时,它返回 null。

问题:为什么第二个参数返回null?

package Assignment_1;

//Begin Class Definition

public class AddressBook {

// Member variables

private String businessPhone;
private String cellPhone;
private String facebookId;
private String firstName;
private String homeAddress;
private String homePhone;
private String lastName;
private String middleName;
private String personalWebSite;
private String skypeId;

//Constructors

public AddressBook (String firstName, String middleName, String lastName, String homeAddress, String businessPhone, String homePhone, String cellPhone, String skypeId, String facebookId, String personalWebSite) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.businessPhone = businessPhone;
this.homePhone = homePhone;
this.cellPhone = cellPhone;
this.skypeId = skypeId;
this.facebookId = facebookId;
this.personalWebSite = personalWebSite;
}

public AddressBook (String firstName) {
this.firstName = firstName;
}

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

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

// Getters and setters

public String getFirstName() {
return firstName;
}

public String getMiddleName() {
return middleName;
}

public String getLastName() {
return lastName;
}

public String getHomeAddress() {
return homeAddress;
}
public String getBusinessPhone() {
return businessPhone;
}

public String getHomePhone() {
return homePhone;
}

public String getCellPhone() {
return cellPhone;
}

public String getSkypeId() {
return skypeId;
}

public String getFacebookId() {
return facebookId;
}

public String getPersonalWebsite() {
return personalWebSite;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setMiddleName(String middleName) {
this.middleName = middleName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}

public void setBusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}

public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}

public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}

public void setSkypeId(String skypeId) {
this.skypeId = skypeId;
}

public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}

public void setPersonalWebSite(String personalWebSite) {
this.personalWebSite = personalWebSite;
}

// Public methods

public static void compareNames(String name1, String name2) {

if(name1.equals(name2)) {
System.out.println(name1);
System.out.println(name2);
System.out.println("The names are the same.");
} else {
System.out.println(name1);
System.out.println(name2);
System.out.println("The names appear to be different.");
}

}

************************************************************
package Assignment_1;

public class BanffMarathonRunner extends AddressBook {

// Member variables
private int time;
private int years;

// Constructors

public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
super(firstName, lastName);
time = min;
years = yr;

}

// Getters and Setters

public int getTime() {
return time;
}

public void setTime(int time) {
this.time = time;
}

public int getYears() {
return years;
}

public void setYears(int years) {
this.years = years;
}

}
************************************************************

package Assignment_1;

import Assignment_1.BanffMarathonRunner;

public class TestBanffMarathonRunner {
public static void main(String[] args) {

BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1);

System.out.print(r1.getLastName());

}

}
}

最佳答案

因为 lastName 为空。

您正在调用 AddressBook(String firstName, String middleName)

并设置 middleName,而不是 lastName

关于java - 对象返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30917078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com