我有一堂课说

Students registeredStudent = null;


由于没有上课,我必须自己写学生课。我似乎无法弄清楚“学生”类中的registeredStudent价值。目前,我的学生课程如下-

public class Students {
String name;
String id;
//boolean selectedStudent;
public Students(String id, String name){
    this.id = id;
    this.name = name;

}

public void display(){
    System.out.println("ID : " + id);
    System.out.println("Name of Student :" + name);

}
}


公共类DrivingSchool {

private Autocar [] ac = new Autocar[10];
private Motorbike [] m = new Motorbike[10];
private Manualcar [] mc = new Manualcar[10];

private Students [] s = new Students[30];

public void start(){
    String[]menu = {"Display all vehicles", "Display all students",
            "Register a new student", "Assign student an auto car", "Assign student a manual car",
            "Assign student a motorbike","Deregister a student"};

    while(true){
        int choice = Keyboard.readInt("Enter Your Choice >");
        if(choice == 1){
            displayAllV();
        }
        else if(choice == 2){

        }
        else if(choice == 3){

        }
        else if(choice == 4){

        }
        else if(choice == 5){

        }
        else if(choice == 6){

        }
        else if(choice == 7){

        }
        else{
            break;
        }
    }



}
public void displayAllV(){
    for(int i = 0;i < ac.length; i++){
        if(ac[i] != null){
        ac[i].display();
        }
    }

    for(int j = 0; j<m.length; j++){
        if(m[j] != null){
            m[j].display();
        }
    }
    for(int x = 0; x < mc.length; x++){
        if(mc[x]!=null){
            mc[x].display();
        }
    }
}
public void displayAllStud(){
    boolean hasStud = false;
    for(int i = 0;i < s.length; i++){
        if(s[i] != null){
            s[i].display();
            hasStud = true;
        }
    }
    if(!hasStud){
        System.out.println("No Student Registered");
    }
}

public void registerStud(){
    String id = Keyboard.readString("Enter Student ID :");
    for(int i = 0;i < s.length; i++){
        if(s[i]!= null && id.equals(s[i].id)){
            System.out.println("Student with this ID already exist");
        return;
        }
    }
    boolean registered = false;
    for(int i =0;i < s.length; i++){
        if(s[i] == null){
            String name = Keyboard.readString("Enter Student name :");
            s[i] = new Students(id, name);
            System.out.println("A new student is registered.");
            registered = true;
            break;
        }
    }
    if(!registered){
        System.out.println("Unable to register. School is full");
    }
}

    public void assignAutoCar(){
        String id = Keyboard.readString("Enter Student ID :");
        Students selectedStudent = null;

        for(int i =0; i<s.length; i++){
            if(s[i] != null && id.equals(s[i].id)){
                selectedStudent = s[i];
                break;
            }
        }

        if(selectedStudent != null && selectedStudent.autocar == null){
for(int i =0; i < ac.length; i++){
                if(ac[i].isVehicleAvailable()){
                    ac[i].setAvail(false);
                    selectedStudent.autoCar = ac[i];
                    break;
                }
    }
    public void createVehicles(){
        ac[0] = new Autocar("LA1", 4);
        ac[1] = new Autocar("LA2", 4);
        ac[2] = new Autocar("LA3", 4);
        ac[3] = new Autocar("LA4", 4);
        ac[4] = new Autocar("LA5", 4);

        mc[0] = new Manualcar("LM1", 4, 5);
        mc[1] = new Manualcar("LM2", 4, 5);
        mc[2] = new Manualcar("LM3", 4, 5);
        mc[3] = new Manualcar("LM4", 4, 5);
        mc[4] = new Manualcar("LM5", 4, 5);

        m[0] = new Motorbike("M1", 2);
        m[1] = new Motorbike("M2", 2);
        m[2] = new Motorbike("M3", 3);
        m[3] = new Motorbike("M4", 3);
        m[4] = new Motorbike("M5", 4);
    }



}

最佳答案

好的,首先,registeredStudent不是您的Student类的一部分。

此作业:

Students registeredStudent = null;


这是主要方法中的内容:嘿,我希望registeredStudent是一个局部变量,它是对Student类型(您的班级)对象的引用,当前使其指向null。

您可以使用以下方法将registeredStudent指向实际的Student对象:

registeredStudent = new Students("1", "Mike");


因此,现在,registeredStudent指向一个新的Student对象,该对象的ID为1,名称为Mike。

因此,registeredStudent不属于Student类。它只是main方法中的局部变量。

(注意:最好以单数形式命名课程:例如,学生而不是学生。当您要创建新的学生对象时,新学生(...)比新学生(...)更有意义

09-25 21:35