- public class Student{
- private static String name,
- EnglishScore,
- MathScore,
- ChineseScore;
- //有参构造函数
- public Student(String name, String englishScore, String mathScore,
- String chineseScore) {
- Student.name = name;
- Student.EnglishScore = englishScore;
- Student.MathScore = mathScore;
- Student.ChineseScore = chineseScore;
- }
复制代码
你在这里将类的成员变量定义成静态的,成员变量就变成了共享数据,也就是说,不是每一个对象都有一组这样的数据了,而是所有的对象共同的拥有这些数据,一个对象改变数据,其他对象调用时,也是改变完的相应数据。
- Student[] stus = new Student[5];
- stus[0] = new Student("LiLei","30","23","20");//这里将 name设为LiLei,成绩分别是30,23,20;
- stus[1] = new Student("Hanmei","23","40","23");//这里又将name设为了Hanmei,成绩23,40,23;
- stus[2] = new Student("Jim","32","20","24");//................
- stus[3] = new Student("Lily","56","24","27");//...............
- stus[4] = new Student("Lucy","34","67","28");//.................
复制代码
最后也就是你将共有数据设置为name="Lucy", EnglishScore="34", MathScore,="67" ChineseScore="28";
等你在输入时看到的也就是最后一次设置值(最后一次调用够着函数是给的值)了。
把- private static String name, EnglishScore, MathScore, ChineseScore;
复制代码 中的static删了就可以了。 |