如何在java中返回匿名实例化对象

时间:2016-04-06 02:11:40

标签: java object return anonymous

在步骤4,我必须使用4个输入的信息项返回一个匿名实例化的Student对象。由于我找不到任何论坛来解决这个问题,我需要一些帮助来设置它或一个例子。

import java.util.Scanner;

public class Students
{
  private static Scanner input = new Scanner(System.in);

  public static void main(String[] args)
  {
    Student[] students;

    students = getStudents();
    printStudents(students);
  }

  private static Student[] getStudents()
  {
    Student[] temp;
    int       how_many;

    System.out.print("How many students? ");
    how_many = input.nextInt();
    purgeInputBuffer();
    temp =  new Student[input.nextInt()];  // Step 1 
    for (int i = 0; i < temp.length; i++)
    {
      getStudent();
      temp[i] = getStudent();     // Step 2
    }
    return temp;    // Step 3
  }

  private static Student getStudent()
  {
    String name,
           address,
           major;
    double gpa;

    System.out.print("Enter name: ");
    name = input.nextLine();
    System.out.print("Enter address: ");
    address = input.nextLine();
    System.out.print("Enter major: ");
    major = input.nextLine();
    System.out.print("Enter GPA: ");
    gpa = input.nextDouble();
    purgeInputBuffer();

    return ___________________________________________________;     // Step 4
  }

  private static void printStudents(Student[] s)
  {
    System.out.println();
    for (int i = 0; i < s.length; i++)    // Step 5
    {
      System.out.println(______);     // Step 6
    }
  }

  private static void purgeInputBuffer()
  {
    // ----------------------------------------------------
    // Purge input buffer by reading and ignoring remaining
    // characters in input buffer including the newline
    // ----------------------------------------------------
    input.nextLine();
  }
}

2 个答案:

答案 0 :(得分:3)

简单地

return new Student(constructor args);

其中constructor argsStudent构造函数所需的任何参数。

这里使用“匿名”不是标准的Java术语。我想,因为你没有将对象引用分配给局部变量,所以它可以被认为是“匿名的”。只要getStudent()在<{p}}中调用getStudents(),它就不会保持匿名状态

temp[i] = getStudent();

所以引用将立即保存(进入数组)。

“匿名”出现在术语“匿名子类”中,但这是一个完全不同的概念,您可能尚未涉及。

答案 1 :(得分:0)

您可以将字段设为私有,并使用参数化构造函数对其进行初始化。

public class Students
{
  private static Scanner input = new Scanner(System.in);
  private String name;
  private String address;
  private String major;
  double gpa;

  public Students(String name, String address, String major, double gpa) {
     this.name = name;
     this.address = address;
     this.gpa = gpa;
     this.major = major;
  }

  public static void main(String[] args)
  {
    Student[] students;

    students = getStudents();
    printStudents(students);
  }

  private static Student[] getStudents()
  {
    Student[] temp;
    int       how_many;

    System.out.print("How many students? ");
    how_many = input.nextInt();
    purgeInputBuffer();
    temp =  new Student[input.nextInt()];  // Step 1 
    for (int i = 0; i < temp.length; i++)
    {
      getStudent();
      temp[i] = getStudent();     // Step 2
    }
    return temp;    // Step 3
  }

  private static Student getStudent()
  {
    String name,
           address,
           major;
    double gpa;

    System.out.print("Enter name: ");
    name = input.nextLine();
    System.out.print("Enter address: ");
    address = input.nextLine();
    System.out.print("Enter major: ");
    major = input.nextLine();
    System.out.print("Enter GPA: ");
    gpa = input.nextDouble();
    purgeInputBuffer();

    return  new Students(name,address,major,gpa);    // Step 4
  }

  private static void printStudents(Student[] s)
  {
    System.out.println();
    for (int i = 0; i < s.length; i++)    // Step 5
    {
      System.out.println(______);     // Step 6
    }
  }

  private static void purgeInputBuffer()
  {
    // ----------------------------------------------------
    // Purge input buffer by reading and ignoring remaining
    // characters in input buffer including the newline
    // ----------------------------------------------------
    input.nextLine();
  }
}