2016-04-06 24 views
2

In Schritt 4 muss ich ein anonymes instanziiertes Student-Objekt mithilfe der 4 eingegebenen Informationen zurückgeben. Da ich keine Foren finden konnte, die sich damit beschäftigen, brauche ich Hilfe bei der Einrichtung oder ein Beispiel.Zurückgeben eines anonymen instanziierten Objekts in 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(); 
    } 
} 

Antwort

3

einfach

return new Student(constructor args); 

wo constructor args sind, was Argumente Ihre Student Konstruktor erfordert.

Die Verwendung von "anonymous" hier ist nicht Standard-Java-Terminologie. Ich nehme an, da Sie die Objektreferenz nicht einer lokalen Variablen zuweisen, könnte sie als "anonym" betrachtet werden. Es wird nicht anonym bleiben für lange, da getStudent() in getStudents() bei

temp[i] = getStudent(); 

so die Referenz sofort gespeichert werden (in das Array) genannt wird.

"Anonymous" tritt in dem Begriff "anonyme Unterklasse" auf, aber das ist ein völlig anderes Konzept, das Sie wahrscheinlich noch nicht berührt haben.

+0

danke für die Klarstellung, dachte nicht darüber auf diese Weise – Beeeee

0

Sie können die Felder privat machen und einen parametrisierten Konstruktor verwenden, um sie zu initialisieren.

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