2016-04-06 53 views
2

在第4步,我必须使用4个输入的信息返回一个匿名实例化的Student对象。因为我找不到任何论坛来解决这个问题,所以我需要一些帮助来设置它或者一个例子。如何返回java中的匿名实例化对象

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

回答

3

只需

return new Student(constructor args); 

其中constructor args是什么参数你Student构造要求。

这里使用“匿名”不是标准的Java术语。我猜想,既然你不把对象引用赋值给一个局部变量,它可以被认为是“匿名的”。它不会在

temp[i] = getStudent(); 

所以将参考立即保存(到阵列),用于长因为getStudent()被称为在getStudents()保持匿名。

“匿名”出现在术语“匿名子类”中,但这是一个完全不同的概念,你可能还没有涉及到。

+0

感谢您的澄清,没有这样想过 – Beeeee

0

您可以将这些字段设置为private,并使用参数化构造函数来初始化它。

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