繁体   English   中英

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

[英]How to return an anonymous instantiated object in java

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

只是

return new Student(constructor args);

constructor argsStudent构造函数需要的任何参数。

这里使用“匿名”不是标准的Java术语。 我想因为您没有将对象引用分配给局部变量,所以可以将其视为“匿名”。 由于在以下位置的getStudent()中调用了getStudents() ,因此它不会长时间保持匿名状态

temp[i] = getStudent();

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM