我正在寫一個bluej程序,它由幾個獲取用戶輸入並將它們保存爲字符串數據的類組成。這些類會覆蓋其他方法,並且將顯示在名爲CollegeList的最終類中。然而,對於CollegeList類我不允許在我的任務中擴展這些子類。相反,我打算使用bluej'使用關係'並在每個循環中輸出這些類的輸入和輸出。如何才能做到這一點?這裏是我的一些代碼:java使用關係
// College List
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class CollegeList
{
//Input Reader
Scanner scanner = new Scanner(System.in);
private ArrayList<Person> people;
//Main Public Method
public CollegeList()
{
people=new ArrayList<Person>();
}
public void main()//not allowed to extend
{
dataEntry();
}
public void getPeople(Person persons)
{
people.add(persons);
System.out.println(people);;
}
//*** Attempting to output preceding class Person in loop - Must be done this way
public void dataEntry()
{
for(Person persons: people)
{
persons.dataEntry();
}
}
}
的Person
類:
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Person extends Student
{
// instance variables for data types
public ArrayList<String> firstName;
public ArrayList<String> lastName;
public ArrayList<String> streetAdress;
public ArrayList<String> postCode;
public ArrayList<String> phoneNumber;
Scanner scanner = new Scanner(System.in);
/**
* Constructor for objects personal details.
*/
public Person()
{
firstName = new ArrayList<String>();
lastName = new ArrayList <String>();
streetAdress = new ArrayList<String>();
postCode = new ArrayList<String>();
phoneNumber = new ArrayList<String>();
}
/**
* Allows User to Enter Details into class Person and Displays it.
*/
public void dataEntry()
{
System.out.print("Enter First Name: ");
firstName.add(scanner.nextLine());
System.out.print("Enter Last Name: ");
lastName.add(scanner.nextLine());
System.out.print("Enter Street Adress: ");
streetAdress.add(scanner.nextLine());
System.out.print("Enter Post Code: ");
postCode.add(scanner.nextLine());
System.out.print("Enter Phone Number: ");
phoneNumber.add(scanner.nextLine());
//display persons information on single line
System.out.println("The details are - " +
"Name: " +
firstName + "," +
"Surname: " +
lastName + "," +
"Street Adress: " +
streetAdress + "," +
"Post Code: " +
postCode + "," +
"Phone Number: " +
phoneNumber + ".");
}
}
請再次解釋你正在努力完成什麼? – tgoossens
當然,我最初只是擴展了頂級類來獲取它的方法內容,但我的講師似乎希望我使用for循環來獲取「person」的內容。 –