参考文献没有得到更新传递给一个类之后?

时间:2011-06-23 04:56:09

标签: java oop

class Main
{

    public static void main()
    {
        Student one=new Student();
        one.setName("firstname");

        AllStudent all=new AllStudent();
        all.add(one);

        // Now changing name of student

        one.setName("secondname");

        // Here I getAll Students

        Collection<Student> cs=all.getAll();


            java.util.Iterator<Student> itr=cs.iterator();


            while(itr.hasNext()){
                Student rgc=itr.next();
                System.out.println(rgc.getName());
            }
    }

    clas public Student
    {

        String name;

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

        public String getName()
        {
            return name;
        }
    }

    clas public AllStudent
    {

        Collection<Student> stds;

        public void addAll(Collection<Student> stds)
        {
            this.stds=stds;
        }

        public void add(Student std)
        {
            stds.add(std);
        }

        public Collection<Student> getAll()
        {
            return stds;
        }
    }
}

现在这将给我“名字”。

但我想得到“第二名”。那我该怎么办?如果API有任何变化或创建新类?

3 个答案:

答案 0 :(得分:1)

问题是你实际上没有编写一个编译和运行的程序。如果您修复了编译错误,请更改main的签名并初始化您的集合,您会发现该程序始终打印

secondname

这就是你想要的。

答案 1 :(得分:1)

public class Student
{
    private String name;
    public Student(String pName)
    {
        name = pName;
    }

    public void setName(string pName)
    {
       name = pName;
    }

    public String getName()
    {
       return name;
    }

    public String toString()
    {
        return name;
    }
}

public class StudentCollection
{
    private LinkedList<Student> studs;

    public StudentCollection()
    {
       studs = new LinkedList<Student>();
    }

    public void add(Student stud)
    {
       if (stud != null)
       {
           studs.addLast(stud);
       }
    }
}

public class Driver
{
    public static void main(String[] args)
    {
        Student one = new Student("Matt");
        StudentCollection students = new StudentCollection();
        students.add(one);

        one.setName("Kyle");
        for(Student stud : students)
        {
             System.out.println(stud);
        }
    }
}

输出:

凯尔

答案 2 :(得分:1)

尝试这是你的代码,我只是在那个

中实现了一些东西
package pkg;

import java.util.Collection;
import java.util.HashSet;

public class Main {
    public static void main(String arg[]){
        Student one=new Student();
        one.setName("firstname");

        AllStudent all=new AllStudent();
        all.add(one);

        // Now changing name of student

        one.setName("secondname");

        // Here I getAll Students

        Collection<Student> cs=all.getAll();

        java.util.Iterator<Student> itr=cs.iterator();

        while(itr.hasNext()){
            Student rgc=itr.next();
            System.out.println(rgc.getName());
        }

    }
    static class Student{
        private String name;
        void setName(String name){
            this.name = name;
        }
        String getName(){
            return name;
        }
    }    
    static class AllStudent{
        Collection<Student> stds = new HashSet<Main.Student>();

        void addAll(Collection<Student> stds){
            this.stds = stds;
        }

        void add(Student std){
            this.stds.add(std);
        }
        public Collection<Student> getAll(){
            return stds;
        }

    }
}

我跑了并得到了第二个名字