对象数组索引值被前面的索引值覆盖

时间:2014-06-02 14:22:12

标签: java

对我来说这是一个奇怪的情况,我刚开始学习Java OOP。

我上课看起来像这样

public class Student {
        public static String name;
        public static int marks;

        public Student(String n, int m){
            name = n;
            marks = m;
        }
        public static void printProperties(){
            System.out.println("Name = " + name + " , marks = " + m);
        }
    }

如您所见,构造函数接受两个数据:名称和标记。

在我的主要方法

      System.out.println("Please enter number of students:");
            int n = scan.nextInt();

            Student[] stu = new Student[n];

            String name;
            int marks = 0;

            for(int i = 0; i < stu.length; i++){

                System.out.println("Please enter name for student #" + (i+1));
                name = scan.next();

                System.out.println("Please enter marks for student #" + (i+1));
                marks  = scan.nextInt();

                stu[i] = new Student(name,marks);

                System.out.println();
            }

//Display
            for(int x = 0; x < stu.length; x++){
                System.out.println("#" + (x+1) + " Name: " + stu[x].name + ", Marks: " + stu[x].marks);

            }

所以我的输出如下:

Please enter number of students:
2
Please enter name for student #1
tom
Please enter age for student #1
20

Please enter name for student #2
billy
Please enter age for student #2
80

#1 Name: billy, Marks: 80
#2 Name: billy, Marks: 80

应该是:

   #1 Name: tom, Marks: 20
   #2 Name: billy, Marks: 80

为什么前面的索引值会覆盖其先前的索引值?

4 个答案:

答案 0 :(得分:2)

如果您的Student类看起来像这样,那么您的代码应该可以正常工作:

public class Student{
    String name;
    int marks;
    public Student(String name, int marks){
        this.name = name;
        this.marks = marks;
    }
}

已编辑:

这就是Jon Skeet所说的。

您正在使用类级变量的静态变量,因此每次为它们赋值时都会覆盖它们,并且只保留最后一个值。

这里需要实例变量。

答案 1 :(得分:1)

不要使用静态,简单就是

答案 2 :(得分:1)

不要将您的字段设为static,并让我们使用private来控制访问权限 -

public class Student {
    private String name; // not static, and use private.
    private int marks;

    public Human(String n, int m){
        name = n;
        marks = m;
    }
    public void printProperties(){ // also should not be static.
        System.out.println("Name = " + name + " , marks = " + m);
    }
}

答案 3 :(得分:1)

静态变量属于整个类。它是在所有对象之间共享的一个变量。因此,当您更改该变量时,它会为所有对象更改它。

相反,将namemarks定义为实例变量。换句话说,从变量声明中删除static修饰符。实例变量对每个对象都是唯一的。每个对象都有自己的实例变量副本。

此外,最好将namemarks声明为private。然后为这些变量创建getter和setter。这隐藏了实施。

import java.util.Scanner;

public class Student{

    private String name;
    private int marks;

    public String getName() {  //getter
        return name;
    }

    public void setName(String name) {  //setter
        this.name = name;
    }

    public int getMarks() {  //getter
        return marks;
    }

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

    public Student(String n, int m){
        name = n;
        marks = m;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter number of students:");
        int n = scan.nextInt();

        Student[] stu = new Student[n];

        String name;
        int marks = 0;

        for(int i = 0; i < stu.length; i++){

            System.out.println("Please enter name for student #" + (i+1));
            name = scan.next();

            System.out.println("Please enter marks for student #" + (i+1));
            marks  = scan.nextInt();

            stu[i] = new Student(name,marks);

            System.out.println();
        }

        //Display
        for(int x = 0; x < stu.length; x++){
            System.out.println("#" + (x+1) + " Name: " + stu[x].getName() + ", Marks: " + stu[x].getMarks());

        }

        scan.close();
    }


}