如何在Java中从一个类传递参数到另一个类?

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

How to pass in the parameter from one class to another in Java?

问题

请原谅我如果这是一个重复的问题,但我是Java的初学者,目前正试图从一个叫做Name的类中获取参数值,并将该值传递到另一个叫做Student的类中。

Name类:

public class Name
{
    public String studentName;

    public Name(String fullName)
    {
        studentName = fullName;
    }
}

Student类:

public class Student
{
    private Name studentName;
    private String id;
    private int credits;

    public Student(String studentID)
    {
        studentName = new Name("");
        id = studentID;
        credits = 0;
    }
}

我想要做的是获取在Name类中设置的fullName参数值,并将其传递到Student类中的studentName = new Name("");,而不是传递空字符串以检索名称。

英文:

Forgive me if this is a duplicated question but I'm a beginner in Java and I'm currently trying to get the parameter value from a class called Name and pass that value into another class called Student.

Name Class:

public class Name
{
    public String studentName;


    public Name(String fullName)
    {
        studentName = fullName;
    }
}  

Student Class:

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID)
    {
        studentName = new Name("");
        id = studentID;
        credits = 0;
    }
}

What I want to do is to get the parameter value of fullName which is set in the Name class and pass it in studentName = new Name(""); for the Student class instead passing in an empty string to retrieve the name.

答案1

得分: 2

请问您的代码中的 fullName 参数是指在 Name 类中设置的参数值吗?一个类本身不会有一个值,您需要访问该类的实例。我非常确定您会有某种控制类,例如包含了您的 main() 方法所在的类。

在某一时刻,您会创建一个 Name 类的实例:

Name n = new Name("Brandon");

使用这个 Name 类的实例,您可以访问实际的值:

Student s = new Student("4711", n.studentName);

假设您在 Student 构造函数中也包含了一个额外的参数:

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID, String name)
    {
        studentName = new Name(name);
        id = studentID;
        credits = 0;
    }
}

但是这样会导致您有两个不同的 Name 对象。

另一个选项是将对象本身作为参数传递,这样您的两个对象都引用同一个对象。无论是更改 n.studentName 还是 s.studentName,理论上都会导致另一个对象的值也被更改(不过我还记得在 Java 中关于这个主题有过一些讨论)。

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID, Name nameObject)
    {
        studentName = nameObject;
        id = studentID;
        credits = 0;
    }
}

这个 Student 类可以这样实例化:

Name n = new Name("Brandon");
Student s = new Student("4711", n);

您绝对应该开始阅读关于面向对象编程的介绍,因为在这几行代码中已经有相当多的误解了。类和对象之间的区别是至关重要的,此外,在这些场景中通常会在类中使用 getter 和 setter 而不是公共变量。要实现您想要的依赖关系,您可能需要了解面向对象编程中的组合和聚合。此外,传值调用和传引用调用之间的区别也值得研究一下。

英文:

What do you mean by "taking the parameter value of fullName, which is set in Name class"? A class will not have a value, you need access to the instance of this class. I'm pretty sure you will have some kind of control class, e.g. where your main() resides.

At some point you will have created a Name instance:

Name n = new Name("Brandon");

Using this instance of Name class, you can access the actual value:

Student s = new Student("4711", n.studentName);

, assuming you also have included an additional parameter in your Student constructor:

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID, String name)
    {
        studentName = new Name(name);
        id = studentID;
        credits = 0;
    }
}

, but this would result in you having 2 different Name objects.

Another option is to pass the object itself as parameter, so both of your objects reference to same object. Changing studentName of either n.studentName and s.studentName would in theoretically result in the value of the respective other being changed as well (I can recall some discussions regarding that topic in Java though).

public class Student
{
    private Name studentName;
    private String id;
    private int credits;
    
    public Student(String studentID, Name nameObject)
    {
        studentName = nameObject;
        id = studentID;
        credits = 0;
    }
}

, which is instantiated by

Name n = new Name("Brandon");
Student s = new Student("4711", n);

You should definitely start reading introductions into object oriented programming, as there are quite a lot of misassumptions just in those few lines of code. The difference between class and object is crucial, also it's usual in those scenarios to have getters/setters rather than public variables in classes. To achieve the kind of dependency you want to have, you might want to look into composition and aggregation in the context of object-orientation. Also the difference between pass-by-value and pass-by-reference is worth looking into.

huangapple
  • 本文由 发表于 2020年10月16日 22:20:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64390998.html
匿名

发表评论

匿名网友

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

确定