如何从超类中调用toString

时间:2017-04-08 01:00:10

标签: java tostring

我必须制作程序,询问用户姓名,地址,电话号码,客户编号,以及客户是否希望添加到邮件列表(显示为真或假),然后显示用户信息我完整地完成了整个程序唯一的问题是我的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;
    }    

2 个答案:

答案 0 :(得分:1)

只需使用超级关键字来调用父类。

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

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

答案 1 :(得分:0)

eeeemmm只需将其设为System.out.print - 而不是System.out.println 在这些方面

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