java使用关系

时间:2012-06-01 07:07:07

标签: java extends bluej

我正在用bluej编写一个程序,它由几个获取用户输入的类组成,并将它们保存为String数据。这些类重写了彼此的方法,并且意味着显示在名为CollegeList的最终类中。但是,对于类CollegeList,我不允许在我的任务中扩展这些子类。相反,我打算使用bluej'使用关系'并在每个循环中输出这些类输入和输出。如何才能做到这一点?这是我的一些代码:

// 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 + "." );
    }
}

1 个答案:

答案 0 :(得分:2)

我从您的问题中了解到,您被要求使用合成而不是继承

此外,您还应该纠正一些基本问题:

  • Person课程不应该ListfirstName, lastName等。它应该让它们变得简单String,然后您可以创建List根据需要Person。然后,您可以使用 for loop 根据需要迭代此列表。

  • Person不应该延伸Student,因为逻辑上每个人都不是学生。