gpt4 book ai didi

java - 如何从父类(super class)调用 toString

转载 作者:行者123 更新时间:2023-11-30 06:48:20 24 4
gpt4 key购买 nike

我必须编写程序,询问用户姓名、地址、电话号码、客户号码以及客户是否愿意添加到邮件列表(显示为 true 或 false),然后显示用户信息我已经让整个程序完美运行,唯一的问题是我的两个 toString() 方法之间有一个空格。该作业要求我将整个程序放在一个文件中,没有空格,这是一个示例输出:

示例

Customer:
Name: Julia Stevens
Address: 77 Massachusetts Ave Cambridge, MA 02139
Phone·Number: 617-777-7777
Customer·Number: 928734502
Recieve·Mail?: false

这是我的代码:代码

public static void main(String[] args) 
{
String personInput;
String customerInput;
Person A = new Person("", "", "");
Customer B = new Customer("", false, "", "", "");

Scanner input = new Scanner(System.in);

System.out.print("Enter name of customer:");
personInput = input.nextLine();
A.setName(personInput);
System.out.print("Enter address of customer:");
personInput = input.nextLine();
A.setAddress(personInput);
System.out.print("Enter phone number of customer:");
personInput = input.nextLine();
A.settNumber(personInput);
System.out.print("Enter customer number:");
customerInput = input.nextLine();
B.setcNumber(customerInput);
System.out.print("Enter yes/no -- does the customer want to receive mail?:");
customerInput = input.nextLine();
B.setmList(false);

if (customerInput.equals("yes"))
B.setmList(true);
else if (customerInput.equals("no"))
B.setmList(false);

System.out.println("");

System.out.println("Cutomer: ");
System.out.println(A.toString());
System.out.println(B.toString());

}

private static class Person
{
private String name;
private String address;
private String tNumber;

public Person(String name, String address, String tNumber)
{
this.name = name;
this.address = address;
this.tNumber = tNumber;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getAddress()
{
return address;
}

public void setAddress(String address)
{
this.address = address;
}

public String gettNumber()
{
return tNumber;
}

public void settNumber(String tNumber)
{
this.tNumber = tNumber;
}

@Override
public String toString() {
return "Name: " + name + "\n"
+ "Address: " + address + "\n"
+ "Phone Number: " + tNumber + "\n";
}
}
private static class Customer extends Person
{
private String cNumber;
private boolean mList;

public Customer(String cNumber, boolean mList, String name, String address, String tNumber)
{
super(name, address, tNumber);
this.cNumber = cNumber;
this.mList = mList;
}

public String getcNumber()
{
return cNumber;
}

public void setcNumber(String cNumber)
{
this.cNumber = cNumber;
}

public boolean ismList()
{
return mList;
}

public void setmList(boolean mList)
{
this.mList = mList;
}

@Override
public String toString() {
return "Customer Number: " + cNumber + "\n"
+ "Receive Mail?: " + mList;
}

最佳答案

只需使用super关键字来调用父类即可。

    @Override
public String toString() {
return super.toString() + "\n Customer Number: " + cNumber + "\n"
+ "Receive Mail?: " + mList;
}

您可以使用关键字 super 访问所有属性和方法,只需要正确的访问说明符即可!

关于java - 如何从父类(super class)调用 toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43289080/

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