java,获取set方法 [英] java, get set methods

查看:286
本文介绍了java,获取set方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题之前已经被问过了,但即使在阅读后:

JavaGet和Set方法



Java Get / Set方法返回null

更多我还是不明白如何解决我的问题。



使用另一个类的get方法访问类中的变量时,我收到值为null。



我如何接收我的正确值而不是null?




这是我尝试从我的变量中获取变量的类(并非包含所有变量)。

  public class RLS_character_panel extends javax.swing.JPanel implements ActionListener,ItemListener {

private String name1 = 你好;

public String getName1(){
return name1;
}

public void setName1(String name1){
this.name1 = name1;
}

}






这是我尝试获取值TO的类。该类扩展了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);

//构造函数
public RLS_strid_java(字符串标题){
super(titel);
this.setSize(1000,772);
this.setVisible(true);
this.setResizable(false);
this.add(p);


$ / code $ / pre

$ hr

Jpanel Displays null。

解决方案

为了理解get和set,它们都与变量在不同类之间传递的方式有关。

get方法用于从类中获取或检索特定的变量值。



设置值用于存储变量。



get和set的整点是检索并存储相应的数据值。

我在这个旧项目中做了什么,我有一个User类,其中包含我在Server类中使用的get和set方法。

User类的get方法:

  public int getuserID )
{
//获取userID变量实例
返回userID;
}
public String getfirstName()
{
//获取firstName变量实例
返回firstName;
}
public String getlastName()
{
//获取lastName变量实例
返回lastName;
}
public int getage()
{
//获取年龄变量实例
返回年龄;
}

public void setuserID(int userID)
{
//设置userID变量值
this.userID = userID;
}
public void setfirstName(String firstName)
{
//设置firstName变量文本
this.firstName = firstName;
}
public void setlastName(String lastName)
{
//设置lastName变量文本
this.lastName = lastName;
}
public void setage(int age)
{
//设置年龄变量值
this.age = age;


然后在 run()方法在我的Server类中,如下所示:

  //创建用户对象
User use = new User(userID,firstName,lastName,age);
//用于设置用户对象的Mutator方法
use.setuserID(userID);
use.setlastName(lastName);
use.setfirstName(firstName);
use.setage(age);


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

Java "Get" and "Set" Methods

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.

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

这篇关于java,获取set方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆