简体   繁体   English

将信息从一个班级调用到另一班级?

[英]Calling the info from one class to another class?

I am relatively new to programming and an working with setters and getters at the moment.我目前对编程和使用 setter 和 getter 的工作还比较陌生。

I have something set up where I have a student class that has information about said student, including their first, middle, and last name, their student ID, and their major.我有一些设置,我有一个学生班级,其中包含有关所述学生的信息,包括他们的名字、中间名和姓氏、他们的学生 ID 和他们的专业。

I need to set it so that, if their student ID is less than zero, it automatically sets it to -1.我需要设置它,如果他们的学生 ID 小于零,它会自动将其设置为 -1。 I also need to set the major to undecided if they do not input anything.如果他们不输入任何内容,我还需要将主要设置为未定。

I also need to override the toString method and print all of this information out.我还需要覆盖 toString 方法并将所有这些信息打印出来。

I feel like I have the first part with the names down, I am not sure about the rest of it however.我觉得我有第一部分的名字,但我不确定其余部分。 I am not sure how I am supposed to use the toString method while also using setters and getters.我不确定在使用 setter 和 getter 的同时应该如何使用 toString 方法。

Below is my Student class that does all of the work.下面是我完成所有工作的 Student 类。

import java.util.Objects;
import java.util.Scanner;
public class Student {
        String first;
        String middle;
        String last;
        String major = "Undecided";
        static int studentID = -1;

        public Student(String first, String middle, String last) {
                Objects.requireNonNull(first);
                Objects.requireNonNull(last);
        }

        public void setFirst(String A) {
                first = A;
        }

        public void setMiddle(String B) {
                middle = B;
         }

        public void setLast(String C) {
                last = C;
        }

        private String getFirst() {
                return first;
        }

        private String getMiddle() {
                return middle;

        }

        private String getLast() {
                return last;
        }

        private String getMajor() {
            return major;
            
        }
        public void setMajor(){
        
            
        
        static void register(int a){
                if (a < 0) {
                    studentID = a;
                } else {
                    studentID = getID(a);
                }
        }

        private static int getID(int a) {
                if (studentIDInput < 0) {
                    studentID = -1;
                } else {
                    studentID = a;
                }
            return studentID;
        }

        public static void main(String[] args) {
                String first = "abc";
                String middle = "def";
                String last = "ghi";

                Scanner sc = new Scanner(System.in);
                String majorInput = sc.next();
                int studentIDInput = sc.nextInt();

                Student student1 = new Student(first, middle, last);

                System.out.println(student1.getFirst().toString() + " " + student1.getMiddle().toString() + " " + student1.getLast().toString() + '\n' + "Major:" + " " + student1.getMajor().toString() + '\n' );
        }
        
        @Override
        public String toString() {
            return ;
            
        }

}

I have also included the Driver class just for reference.我还包含了 Driver 类,仅供参考。


public class Driver {
        static String first;
        static String middle;
        static String last;


        public static void main(String[] args){
                Student student1 = new Student(first, middle, last);
                student1.setFirst("Mikayla");
                student1.setMiddle("Rose");
                student1.setLast("Knox");


        }
}

You have this constructor:你有这个构造函数:

public Student(String first, String middle, String last) {
    Objects.requireNonNull(first);
    Objects.requireNonNull(last);
}

It does its job of checking that first and last name are not null , but it does not do anything with the values besides checking.它的工作是检查名字和姓氏是否不为null ,但除了检查之外,它不会对值做任何事情。 The constructor's job is to construct the object, ie, initialize its member variables.构造函数的工作是构造对象,即初始化其成员变量。 When your constructor is done, you should have a usable object, without having to call any setters in it.当你的构造函数完成后,你应该有一个可用的对象,而不必在其中调用任何 setter。

You need to add that:你需要补充一点:

public Student(String first, String middle, String last) {
    Objects.requireNonNull(first);
    Objects.requireNonNull(last);
    this.first = first;
    this.middle = middle;
    this.last = last;
}

Note that you don't need to use setters here as code within the class can access member variables directly.请注意,这里不需要使用 setter,因为类中的代码可以直接访问成员变量。 You can use setters if you want, though.不过,您可以根据需要使用 setter。

As for toString : this is a method mainly used in debugging, and it displays some helpful information about the object it's called on.至于toString :这是一个主要用于调试的方法,它显示有关它被调用的对象的一些有用信息。 You could implement it like below, with a bit of ?: to make sure to only print the middle name if it's not null :你可以像下面那样实现它,用一点?:确保只打印中间名,如果它不为null

@Override
public String toString() {
    return first + " " + (middle != null ? middle + " " : "") + last;
}

I'll leave it to you to also include major and ID.我会把它留给你还包括专业和 ID。

On using a Scanner : You use a Scanner to get input from somewhere, like the from the user.使用Scanner :您使用扫描仪从某处获取输入,例如来自用户的输入。 You don't need it in toString or any setters or getters.toString或任何 setter 或 getter 中不需要它。 These are all methods that should be very simple and not deal with I/O classes like Scanner .这些都是应该非常简单的方法,而不是像Scanner这样的 I/O 类。

If you are using constructors, you do not really need setters.如果您正在使用构造函数,则实际上并不需要 setter。 Try something like this:尝试这样的事情:

class Student {
    private String first;
    private String middle;
    private String last;
    private String major;
    private int studentID;
    
    public Student(String first, String middle, String last) {
        this(first, middle, last, "undecided", -1);
    }
    
    public Student(String first, String middle, String last, String major, int studentID) {
        this.first = first;
        this.middle = middle;
        this.last = last;
        this.major = major;
        this.studentID = studentID;
    }
    
    @Override
    public String toString() {
        return "first: " + first + "\nmiddle: " + middle + "\nlast: " + last + "\nmajor: " + major + "\nid: " _ studentID;
    }
}

This way, when you create a new Student object with 3 parameters, the last 2 are automatically set to "undecided" and -1.这样,当您创建具有 3 个参数的新 Student 对象时,最后 2 个参数会自动设置为“未定”和 -1。 If there is a case when you have the ID and not the major (or the other way around), you can add more constructors.如果出现您拥有 ID 而不是主要(或相反)的情况,您可以添加更多构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM