2014-01-27 48 views
-1

我不完全明白我在做什麼以及我做錯了什麼。 請幫我修復/確定我的代碼。 我應該創建至少3個Student對象,其中包含您選擇的輸入數據,以使用類的構造函數初始化Student對象的所有數據字段。 聲明一個ArrayList對象來保存Student對象。 將Student對象添加到ArrayList對象。 調用Student類的toString方法以使用ArrayList對象中的Student對象打印學生的全名,後跟出生日期和每個學生的地址。驅動程序代碼:需要幫助/指導

正如你所看到的,即使有大量的研究,我也不知道如何解決這個問題。我仍然處於初級水平。

import java.util.ArrayList; 

public class PersonDriver { 
    public static void main(String[] args) { 
     Person toString = new Person(); 
     Person middleInitial = new Person(); 
     Person lastName = new Person(); 
     ArrayList<String> studentList = new ArrayList<String>(); 

     studentList.add(new String("John", "Cassy")); 
     studentList.add(new String("Jessie", "Lucy")); 
    for (String student : studentList) { 
    System.out.println(student); } 
    } 
} //end PersonDriver class 

這是上面的驅動程序類的原代碼:Constructor requiring more than one for subclass super

+1

你到底在做什麼? – George

+0

你不應該爲此打開一個新問題。你不能在你之前的問題中問過嗎? – Leo

+0

我認爲這個問題應該被遷移到http://codereview.stackexchange.com/ – Barranka

回答

0

對於每個實例化的學生,你需要一個Person類。

由於Person Object有一個像

public Person(String lastName, String middleInitial, String firstName)

構造然後實例化一個Person它看起來像這樣

人學生=新的Person( 「Blogg」, 「F」,「喬「);

這現在可以添加到studentList

studentList.add (student); 

現在把上面的成環

當你有一個方法getName,你可以使用它作爲

for (Person std: studentList) { 
    System.out.println(std.getName()); 
}