【问题标题】:How to return an anonymous instantiated object in java如何在java中返回一个匿名实例化对象
【发布时间】:2016-07-26 05:07:53
【问题描述】:

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

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

【问题讨论】:

    标签: java object return anonymous


    【解决方案1】:

    简单

    return new Student(constructor args);
    

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

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

    temp[i] = getStudent();
    

    所以引用将立即保存(到数组中)。

    “匿名”出现在“匿名子类”一词中,但这是一个完全不同的概念,您可能还没有接触过。

    【讨论】:

    • 谢谢你的澄清,没想到那样
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2015-04-09
      相关资源
      最近更新 更多