从一个类调用另一个类中的信息?

huangapple go评论62阅读模式
英文:

Calling the info from one class to another class?

问题

我相对于编程还比较新,在处理设置器和获取器。我目前设置了一个学生类,其中包含有关该学生的信息,包括他们的名字(名、中间名和姓)、学生ID和专业。

我需要这样设置,如果他们的学生ID小于零,它会自动设置为-1。如果他们没有输入任何内容,我还需要将专业设置为"undecided"。

我还需要重写toString方法并打印出所有这些信息。

我觉得我已经处理了关于名字的第一部分,但是其他部分我不确定。我不知道在同时使用设置器、获取器和toString方法时应该如何操作。

以下是执行所有这些工作的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 ;

    }

}

我还包括了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");
    }
}

请注意,您的代码中还有一些需要补充或更正的部分,以使其能够正确运行和实现您的目标。

英文:

I am relatively new to programming and an working with setters and getters at the moment.

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.

I need to set it so that, if their student ID is less than zero, it automatically sets it to -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.

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.

Below is my Student class that does all of the work.

import java.util.Objects;
import java.util.Scanner;
public class Student {
String first;
String middle;
String last;
String major = &quot;Undecided&quot;;
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 &lt; 0) {
studentID = a;
} else {
studentID = getID(a);
}
}
private static int getID(int a) {
if (studentIDInput &lt; 0) {
studentID = -1;
} else {
studentID = a;
}
return studentID;
}
public static void main(String[] args) {
String first = &quot;abc&quot;;
String middle = &quot;def&quot;;
String last = &quot;ghi&quot;;
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() + &quot; &quot; + student1.getMiddle().toString() + &quot; &quot; + student1.getLast().toString() + &#39;\n&#39; + &quot;Major:&quot; + &quot; &quot; + student1.getMajor().toString() + &#39;\n&#39; );
}
@Override
public String toString() {
return ;
}
}

I have also included the Driver class just for reference.


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(&quot;Mikayla&quot;);
student1.setMiddle(&quot;Rose&quot;);
student1.setLast(&quot;Knox&quot;);
}
}

答案1

得分: 1

你有这个构造函数:

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

它的作用是检查 firstlast 名称是否不为 null,但是它除了进行检查之外并没有对这些值做任何操作。构造函数的任务是构建对象,即初始化其成员变量。当构造函数完成时,你应该拥有一个可用的对象,而无需在其中调用任何设置器。

你需要添加以下内容:

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

注意,在这里你不需要使用设置器,因为类内部的代码可以直接访问成员变量。当然,如果你愿意,你也可以使用设置器。

至于 toString 方法:这是一个主要用于调试的方法,它显示有关调用它的对象的一些有用信息。你可以像下面这样实现它,使用一些 ?: 来确保仅在中间名不为 null 时才打印出中间名:

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

我会让你自行添加专业和学号。

关于使用 Scanner:你可以使用 Scanner 来从某个地方(例如用户输入)获取输入。你不需要在 toString 或任何设置器或获取器中使用它。这些都应该是非常简单的方法,不涉及像 Scanner 这样的 I/O 类。

英文:

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. The constructor's job is to construct the object, i.e, initialize its member variables. When your constructor is done, you should have a usable object, without having to call any setters in it.

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. You can use setters if you want, though.

As for toString: this is a method mainly used in debugging, and it displays some helpful information about the object it's called on. You could implement it like below, with a bit of ?: to make sure to only print the middle name if it's not null:

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

I'll leave it to you to also include major and ID.

On using a Scanner: You use a Scanner to get input from somewhere, like the from the user. You don't need it in toString or any setters or getters. These are all methods that should be very simple and not deal with I/O classes like Scanner.

答案2

得分: 0

如果您使用构造函数,实际上就不需要使用设置器(setters)。可以尝试像这样做:

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;
    }
}

这样,当您使用3个参数创建新的Student对象时,后两个参数会自动设置为"undecided"和-1。如果有一种情况,您只有ID而没有专业(或者反过来),您可以添加更多的构造函数。

英文:

If you are using constructors, you do not really need setters. 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, &quot;undecided&quot;, -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 &quot;first: &quot; + first + &quot;\nmiddle: &quot; + middle + &quot;\nlast: &quot; + last + &quot;\nmajor: &quot; + major + &quot;\nid: &quot; _ studentID;
}
}

This way, when you create a new Student object with 3 parameters, the last 2 are automatically set to "undecided" and -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.

huangapple
  • 本文由 发表于 2020年9月10日 21:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63831276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定