如何 return java 中的匿名实例化对象

How to return an anonymous instantiated object in java

在第 4 步,我必须 return 使用 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();
  }
}

简单

return new Student(constructor args);

其中 constructor args 是您的 Student 构造函数需要的任何参数。

这里使用的 "anonymous" 不是标准的 Java 术语。我想因为您没有将对象引用分配给局部变量,所以它可以被认为是 "anonymous"。它不会长期保持匿名,因为 getStudent()getStudents()

被调用
temp[i] = getStudent();

因此引用将立即保存(到数组中)。

"Anonymous" 出现在术语 "anonymous subclass" 中,但这是一个完全不同的概念,您可能还没有接触过。

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

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();
  }
}