나는 사용자 입력을 얻고 문자열 데이터로 저장하는 여러 클래스로 구성된 bluej로 프로그램을 작성하고 있습니다. 이 클래스는 다른 메소드를 대체하며 CollegeList라는 최종 클래스에 표시됩니다. 그러나 CollegeList 클래스의 경우 이러한 하위 클래스를 확장하기 위해 할당 할 수 없습니다. 대신 나는 bluej 'uses relation'을 사용하고 각 클래스에 대해 이러한 클래스의 입력과 출력을 출력하려고합니다. 어떻게 할 수 있습니까? 여기 내 코드의 일부는 다음과 같습니다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'의 내용을 얻기 위해 for 루프를 사용하기를 원한다. –