2012-03-15 50 views
2

我正在做一個家庭作業項目,需要我從用戶輸入的數據創建一個對象。我有一個名爲Person的類,它接受基本信息,一個名爲Customer的類,它擴展了Person類,幷包含一個客戶號和一個名爲Employee的類,它擴展Person類並返回一個社會安全號。從用戶輸入的數據創建對象

我粘貼了下面我主程序的代碼。我對一些事情有點困惑。首先,當我收集信息(名字,姓氏等),我應該在那裏訪問我的Person類嗎?

第二我猜得更清楚,我該如何創建對象?在所有的我都在線閱讀,我覺得到目前爲止的例子,他們似乎已經進入信息一樣,如果我是把它說

Person firstName = new Person(Jack); 

雖然我收集來自用戶的信息,所以我不看如何分辨它像

Person firstName = new Person (enter info from user here); 

最後又一次,這是一個非常愚蠢的問題,但我必須創建一個接受Person對象的靜態方法。 要創建靜態方法我假設它是

Public Static print() 

,但我怎麼告訴它打印從人類的東西嗎?它是如何知道的?

本書中的大多數示例都包含一個包含所有信息的類,而不是讓用戶輸入它,這讓人感到困惑,因爲現在我被告知用戶可以自由輸入他們想要的內容,而且我需要收集這些信息。

import java.util.Scanner; 
    public class PersonApp 
    { 


public static void main(String[] args) 
{ 
    //welcome user to person tester 
    System.out.println("Welcome to the Person Tester Application"); 
    System.out.println(); 

    Scanner in = new Scanner(System.in); 


    //set choice to y 
    String choice = "y"; 
    while (choice.equalsIgnoreCase("y")) 
    { 

     //prompt user to enter customer or employee 
     System.out.println("Create customer or employee (c/e): "); 
     String input = in.nextLine(); 

     if (input.equalsIgnoreCase("c")) 
     { 
      String firstName = Validator.getString(in, "Enter first name: "); 
      String lastName = Validator.getString(in, "Enter last name: "); 
      String email = Validator.getEmail(in, "Enter email address: "); 
      String custNumber = Validator.getString(in, "Customer number: "); 
     } 

     else if(input.equalsIgnoreCase("e")) 
     { 
      String firstName = Validator.getString(in, "Enter first name: "); 
      String lastName = Validator.getString(in, "Enter last name: "); 
      String email = Validator.getEmail(in, "Enter email address: "); 
      int empSoc = Validator.getInt(in, "Social security number: "); 
     } 


    } 




    System.out.println("Continue? y/n: "); 
    choice = in.next(); 


} 

}

回答

0

首先,我注意到,沒有一個Person對象。我假設你會開始創造它,所以我不會過多地關心自己。

就實際獲取數據而言,您已經達到了一半。根據您要如何構建對象Person,您可以通過傳遞您從用戶處收到的值來創建新對象CustomerEmployee

Customer customer = new Customer(firstName, lastName, email, custNumber); 

Employee employee = new Employee(firstName, lastName, email, empSoc); 

下面是兩者的片段:

public class Person { 

    public Person (String first, String last, String email) { 
     // You'd fill in code here for handling the variables 
    } 

    // ... 
} 

public class Customer extends Person { 

    public Customer (String first, String last, String email, String custNo) { 
     super(first, last, email); 
     // You'd fill in code here for handling the variables 
    } 

    // ... 
} 

public class Employee extends Person { 

    public Employee (int social) { 
     super(first, last, email); 
     // You'd fill in code here for handling the variables 
    } 

    // ... 
} 

要從Person類打印的東西,使用靜態方法(爲什麼你可以重寫toString()?相反),您將其設置爲使您的Person對象具有accesso rs與每個與Person相關的字段。這意味着如果您是員工或客戶,您將擁有與該對象相關的getFirstName()getLastName()等等。 (我把它作爲一個練習給你。)

從這個意義上說,那麼只需要調用這些訪問器來打印值。

public static void print(Person p) { 

    System.out.println(p.getFirstName()) + " " + p.getLastName()); // You can get the trend from here. 
} 
+0

謝謝你的幫助。我將不得不再次閱讀它,但是我很感謝幫助。我在Employee和Customer類中有一個重載toString(),它將包含cusNumber和empSoc。我需要做一些與你發佈的不一樣的東西嗎? – 2012-03-15 14:33:41

+0

'toString()'方法將使打印語句更容易,而無需使用訪問器字段。這是打印關於Object的信息的更好的方法。除非你的任務需要使用static void方法,否則我會選擇'toString()'。 – Makoto 2012-03-15 14:39:01

+0

我想我明白你現在在說什麼。引用她所說的任務:「要將對象的數據打印到控制檯,此應用程序應該使用名爲print的靜態方法來接受Person對象。」 – 2012-03-15 15:02:27

0

要打印Person對象,你可以只使用System.out.println()如果你只是想將它打印到命令行,但你會得到一些不可讀的廢話。
println()方法所做的是,如果對象不是字符串調用,則它的方法是toString(),因爲所有對象都有一個,所以它在java.lang.Object中定義。但是,這種方法給了我們上面提到的不可讀的東西,所以你必須重寫它像做

public class Person 
{ 
    String firstName; 
    String Lastname; 

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

    public String toString() 
    { 
     // Create a String that represents this object 
     return "This person is called " + firstName + " " + lastName; 
    } 
} 

要創建一個對象,你可以在命令行讀取的字符串,然後將它們傳遞到構造函數中誠建議。