簡體   English   中英

構造函數被調用兩次

[英]Constructor gets Called TWice

我正在嘗試實現一個 ArrayList,它保存用戶輸入的詳細信息並顯示它們。 代碼工作正常,但構造函數從 main 調用了兩次,從 StudentDetails 類調用了兩次。 有沒有辦法讓它只調用一次? 這是 Student 類,它有一個調用 StudentDetails 類對象的 main 方法,StudentDetails 類有一個 ArrayList。

public class Student2 {      

  public static void main(String[] args) {              
     StudentDetails sd1 = new StudentDetails();
     sd1.input();
     sd1.display();        
  }

  class StudentDetails {    
    int marks;
    String names;
    List<StudentDetails> sd = new ArrayList<>();

    public int getMarks() {
        return marks;
    }

    public void setMarks(int marks) {
        this.marks = marks;
    }

    public String getNames() {
        return names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    public StudentDetails() {    
        System.out.println("Program Started");
    }

    public void input() {    
        int no;
        StudentDetails sDetails = new StudentDetails();
        System.out.println("How many students?");
        Scanner sc = new Scanner(System.in);
        no = sc.nextInt();

        for (int i = 0; i < no; i++) {
            System.out.println("Enter name of student" + (i + 1));
            sDetails.setNames(sc.next());
            System.out.println("Enter marks for same student");
            sDetails.setMarks(sc.nextInt());
            sd.add(sDetails);    
        }    
    }

    public void display() {
        for (int i = 0; i < sd.size(); i++) {                
            System.out.println("The name of student" + " " + (i + 1) + " " + "is" + " " + sd.get(i).getNames()
                    + " and marks are" + " " + sd.get(i).getMarks());    
        }
    }
}

您調用了兩次(創建兩個StudentDetails實例),實際上這還不夠。 您的input()方法應該多次調用它 - 每次循環迭代一次 - 因為您將這些對象添加到 List 並且您不想多次添加相同的對象。

您可以通過使input()display()靜態方法並將sd更改為靜態變量來避免在main處創建對象。

public static void main(String[] args) {              
   StudentDetails.input();
   StudentDetails.display();        
}

...
static List<StudentDetails> sd = new ArrayList<>();
...
public static void input() {
    ...
    for (int i = 0; i < no; i++) {
        StudentDetails sDetails = new StudentDetails();
        System.out.println("Enter name of student" + (i + 1));
        sDetails.setNames(sc.next());
        System.out.println("Enter marks for same student");
        sDetails.setMarks(sc.nextInt());
        sd.add(sDetails);    
    } 
    ...
}

public static void display() {
    ...
}

這是更新的課程。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Student2 {

public static void main(String[] args) {

    StudentDetails sd1 = new StudentDetails();
    sd1.input();
    sd1.display();

}

class StudentDetails {

    int marks;
    String names;
    List<StudentDetails> sd = new ArrayList<>();

    public int getMarks() {
        return marks;
    }

    public void setMarks(int marks) {
        this.marks = marks;
    }

    public String getNames() {
        return names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    public StudentDetails() {

        System.out.println("Program Started");
    }

    public void input() {

        int no;         
        System.out.println("How many students?");
        Scanner sc = new Scanner(System.in);
        no = sc.nextInt();

        for (int i = 0; i < no; i++) {
            System.out.println("Enter name of student" + (i + 1));
            setNames(sc.next());
            System.out.println("Enter marks for same student");
            setMarks(sc.nextInt());
            sd.add(this);

        }
        sc.close();
    }

    public void display() {
        for (int i = 0; i < sd.size(); i++) {

            System.out.println("The name of student" + " " + (i + 1) + " "
                    + "is" + " " + sd.get(i).getNames() + " and marks are"
                    + " " + sd.get(i).getMarks());

        }
    }
}
}

作為@Eran's answer的一個選項,您可能想要使用更合適的類設計。 目前List<StudentDetails>依賴於StudentDetails一個實例,在我看來,從一開始就沒有意義。

創建一個額外的類作為StudenDetails的管理器

public class Student2 {

    public static void main(String[] args) {
        // We create a Dictionary here now. This holds the StudentDetails now
        StudenDictionary sd1 = new StudenDictionary();
        sd1.input();
        sd1.display();
    }

    static class StudenDictionary {
        List<StudentDetails> sd = new ArrayList<>();

        static Scanner sc = new Scanner(System.in);

        public void input() {
            int no;

            System.out.println("How many students?");
            no = sc.nextInt();

            for (int i = 0; i < no; i++) {
                System.out.println("Enter name of student" + (i + 1));
                // Store in variables and use a proper constructor
                String names = sc.next();
                System.out.println("Enter marks for same student");
                int marks = sc.nextInt();
                // StudenDetails variable in loop now, caring for scope now
                StudentDetails sDetails = new StudentDetails(names, marks);
                sd.add(sDetails);
            }
        }

        public void display() {
            for (int i = 0; i < sd.size(); i++) {
                System.out.println("The name of student" + " " + (i + 1) + " " + "is" + " " + sd.get(i).getNames()
                        + " and marks are" + " " + sd.get(i).getMarks());
            }
        }
    }

    static class StudentDetails {
        int marks;
        String names;

        public int getMarks() {
            return marks;
        }

        public void setMarks(int marks) {
            this.marks = marks;
        }

        public String getNames() {
            return names;
        }

        public void setNames(String names) {
            this.names = names;
        }

        // Use a proper constructor
        public StudentDetails(String names, int marks) {
            this.names = names;
            this.marks = marks;
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM