java,获取设置方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36102768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java, get set methods
提问by Jonatan Jonsson
This question has been asked before, but even after reading:
之前已经问过这个问题,但即使在阅读之后:
Java Get/Set method returns null
And more I still don't understand how to solve my problem.
而且我仍然不明白如何解决我的问题。
When accessing variables in a class using get methods from another class I receive the value null.
当使用来自另一个类的 get 方法访问类中的变量时,我收到值 null。
How do I recieve my correct values instead of null?
如何接收正确的值而不是 null?
This is the class where I try to get my variables FROM (not everything included).
这是我尝试从其中获取变量的类(不包括所有内容)。
public class RLS_character_panel extends javax.swing.JPanel implements ActionListener, ItemListener {
private String name1 = "hello";
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
}
This is the class where i try to get the values TO. This class extends JFrame so that I can add a JPanel which displays the variable. (the JPanel is yet another class called: RLS_strid_panel , which is added upon this frame).
这是我尝试获取值的课程。这个类扩展了 JFrame 以便我可以添加一个显示变量的 JPanel。( JPanel 是另一个名为: RLS_strid_panel 的类,它被添加到这个框架上)。
public class RLS_strid_java extends JFrame {
RLS_character_panel test = new RLS_character_panel();
String name1 = test.getName1();
RLS_strid_panel p = new RLS_strid_panel(namn1);
// constructor
public RLS_strid_java(String titel) {
super(titel);
this.setSize(1000, 772);
this.setVisible(true);
this.setResizable(false);
this.add(p);
}
}
the Jpanel Displays null.
Jpanel 显示为空。
回答by Osiris93
To understand get and set, it's all related to how variables are passed between different classes.
要理解 get 和 set,这都与变量如何在不同类之间传递有关。
The get method is used to obtain or retrieve a particular variable value from a class.
get 方法用于从类中获取或检索特定的变量值。
A set value is used to store the variables.
设置值用于存储变量。
The whole point of the get and set is to retrieve and store the data values accordingly.
get 和 set 的重点是相应地检索和存储数据值。
What I did in this old project was I had a User class with my get and set methods that I used in my Server class.
我在这个旧项目中所做的是我有一个 User 类,其中包含我在 Server 类中使用的 get 和 set 方法。
The User class's get set methods:
User 类的 get set 方法:
public int getuserID()
{
//getting the userID variable instance
return userID;
}
public String getfirstName()
{
//getting the firstName variable instance
return firstName;
}
public String getlastName()
{
//getting the lastName variable instance
return lastName;
}
public int getage()
{
//getting the age variable instance
return age;
}
public void setuserID(int userID)
{
//setting the userID variable value
this.userID = userID;
}
public void setfirstName(String firstName)
{
//setting the firstName variable text
this.firstName = firstName;
}
public void setlastName(String lastName)
{
//setting the lastName variable text
this.lastName = lastName;
}
public void setage(int age)
{
//setting the age variable value
this.age = age;
}
}
Then this was implemented in the run()
method in my Server class as follows:
然后这是run()
在我的 Server 类中的方法中实现的,如下所示:
//creates user object
User use = new User(userID, firstName, lastName, age);
//Mutator methods to set user objects
use.setuserID(userID);
use.setlastName(lastName);
use.setfirstName(firstName);
use.setage(age);
回答by Komgcn
your panel class don't have a constructor that accepts a string
您的面板类没有接受字符串的构造函数
try change
尝试改变
RLS_strid_panel p = new RLS_strid_panel(namn1);
to
到
RLS_strid_panel p = new RLS_strid_panel();
p.setName1(name1);