2017-04-08 74 views
1

我必須製作程序,詢問用戶姓名,地址,電話號碼,客戶號碼以及客戶是否希望添加到郵件列表(顯示爲true或假),然後顯示用戶信息我已經完成了整個程序的工作,唯一的問題是在我的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; 
    }  
+0

'返回super.toString調用()+ 「客戶編號:」 不是的System.out.println + cNumber + 「\ n」 +「接收郵件?」+ mList;' –

+0

@ElliottFrisch沒有工作現在空間已經消失,但名稱,電話號碼和地址現在不顯示 –

回答

0

eeeemmm成全它是System.out.print - 在這些線路上

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

哇,這樣做非常感謝你 –

1

只需使用super keywor d來調用父類。

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

您可以使用關鍵字super訪問所有屬性和方法只需要正確的訪問說明符!

+0

我試過了,但它沒有顯示名稱,地址和電話號碼使用super.toString() –

+0

的事情是關鍵字**靜態** –

+0

以及問題是,該任務要求,它都在同一個文件,所以我不能o如果這是因爲關鍵字static,我將不得不尋找其他解決方法。 –

相關問題