Java 取set值,java,获取set方法

This question has been asked before, but even after reading:

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.

How do I recieve my correct values instead of 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).

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.

解决方案

To understand get and set, it's all related to how variables are passed between different classes.

The get method is used to obtain or retrieve a particular variable value from a class.

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.

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.

The User class's get set methods:

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:

//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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值