【问题标题】:Objects in Linked Lists链表中的对象
【发布时间】:2016-04-10 16:19:46
【问题描述】:

我尝试编写一个包含一些选项的基本菜单的代码。这些选项有以下方法:AddStudent、changeName、setGrade 等。我创建了一个名为 Student 的对象,它有一个名字、一个年级和一个年龄。我想在链接列表中添加学生,但是当我使用添加方法时它不起作用。这是我的代码:

import java.util.*;
class Student {  
    int age;
    int grade;
    String name;
    static LinkedList ll = new LinkedList();
    public Student (String n) {   //we create here a student with a name and an age
        name=n;
        age=0;
        grade=0;
    }
//-------------------------------------------------------------------------
    public void p(String x) {
        System.out.println(x);
    }

    public void addStudent() {
        Scanner s = new Scanner(System.in);
        p("Enter the name that you want");
        String f = s.nextLine();
        Student a = new Student(f);
        ll.add(a);
    }

    public void changeName() {                           //this method is to change the name of a student
        Scanner s = new Scanner(System.in);
        p("Enter whose name you want to change");  
        String c = s.nextLine();
        p("Enter the name that you want");
        String b = s.nextLine();

    }

    public void setGrade(Student a) {                 //this method is to put the student's grade
        Scanner s = new Scanner(System.in);
        p("Enter the grade that you want");
        int g = s.nextInt();
        a.grade=g;
    }

    public void setAge(Student a) {                 //This method is to put the student's grade
        Scanner s = new Scanner(System.in);
        p("Enter the age that you want");
        int h = s.nextInt();
        a.age=h;
    }

    public String getName(Student a) {
        return a.name;
    }

    public int getAge(Student a) {
        return a.age; 
    }

    public int getGrade(Student a) {
        return a.grade; 
    }
}

问题出在 addStudent 的方法上。还有其他方法可以让我制作相同的项目吗?

【问题讨论】:

  • 编译器有什么问题?
  • 遇到什么错误?

标签: java object linked-list


【解决方案1】:

从逻辑上思考这个问题。您有一个代表单个学生的 Student 类。为什么学生会有学生名单?这没有任何意义。

你不会有像Course 这样的程序或可以保存学生列表的程序吗?那是您的列表所属的地方。除非你有令人信服的理由(很少见),否则不要使用静态。

这是Course 类的开始,它使用Student 存储学生信息并将其存储在LinkedList 中的Course 中。您仍然需要实现findStudent 方法,并且可能还需要一个打印列表的方法:

课堂课程:

import java.util.LinkedList;
import java.util.Scanner;

public class Course {
    LinkedList ll = new LinkedList();
    Scanner s = new Scanner(System.in);

    public void addStudent() {
        p("Enter the name that you want");
        String f = s.nextLine();
        Student a = new Student(f);
        ll.add(a);
    }

    public void changeName() {                           //this method is to change the name of a student
        Student student = findStudent();
        p("Enter the name that you want");
        String newName = s.nextLine();
        //student.setName(newName);
    }

    public void setGrade() {                 //this method is to put the student's grade
        Student student = findStudent();
        p("Enter the grade that you want");
        int grade = s.nextInt();
        //student.setGrade(grade);
    }

    public void setAge() {                 //This method is to put the student's grade
        Student student = findStudent();
        p("Enter the age that you want");
        int age = s.nextInt();
       student.setAge(age);
    }

    public Student findStudent(){
        p("Which student did you want to change? Please enter their name:");
        String name = s.nextLine();
        //Find student in the list - left for the author
        Student student = null;
        return student;
    }

    //-------------------------------------------------------------------------
    public void p(String x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        Course course = new Course();
        course.addStudent();
        course.addStudent();
        course.changeName();
        course.setGrade();
    }
}

以及修改后的学生类:

import java.util.*;

public class Student {
    int age;
    int grade;
    String name;

    public Student (String n) {   //we create here a student with a name and an age
        name=n;
        age=0;
        grade=0;
    }

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

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName(Student a) {
        return a.name;
    }

    public int getAge(Student a) {
        return a.age;
    }

    public int getGrade(Student a) {
        return a.grade;
    }

    @Override
    public String toString(){
        return "Student(name:" + name + ", age:" + age + ", grade:" + grade + ")";
    }
}

【讨论】:

    【解决方案2】:

    这段代码有点乱,因为您包含了一些不应出现在Student 类中的代码。例如,addStudent 方法不是静态的,为了调用它,您需要首先实例化Student 的实例并在其上调用该方法。但是,该方法试图要求用户输入新的Student 实例的信息,这是一个糟糕的设计。

    所以,让Student 类只做它应该做的事情。对于您的情况,Student 只需要存储它的年龄、等级和姓名字段并定义构造函数来初始化这些字段,以及可选的 getter 和 setter 方法来根据您的需要设置和检索这些字段。

    您将需要一个“管理器”类来管理您的应用程序。该类将跟踪学生列表,并要求用户输入新学生的信息,然后初始化学生实例并将其放入列表中。这个 Manager 类甚至可以管理您需要接受用户输入或向用户显示信息的 UI。所以,这个类有责任提供一个addStudent 方法。

    Student 类本身应该对应用程序的逻辑一无所知,例如它可能是课程选择程序或其他东西。它只管理自己的信息,而一些管理器类会处理应用程序的逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      相关资源
      最近更新 更多