【问题标题】:java, get set methodsjava, 获取设置方法
【发布时间】:2016-03-19 14:10:47
【问题描述】:

之前有人问过这个问题,但即使在阅读之后:

Java "Get" and "Set" Methods

Java Get/Set method returns null

而且我仍然不明白如何解决我的问题。

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

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


这是我尝试从其中获取变量的类(不包括所有内容)。

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

} 

这是我尝试获取 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); 

    // constructor
    public RLS_strid_java(String titel) { 
        super(titel); 
        this.setSize(1000, 772); 
        this.setVisible(true); 
        this.setResizable(false); 
        this.add(p); 
    } 
}

Jpanel 显示 null。

【问题讨论】:

  • 当你在调试器中单步调试代码时,你能看到哪个值是null吗?
  • 哦,伙计,命名!尊重 Java 命名约定,并为你的类赋予有意义的名称。并向我们​​展示显示值的代码。
  • 你是如何构建“外部”RLS_strid_java 的? “子面板”p 看起来不错,但是您如何从类外调用 RLS_strid_java 的构造函数?
  • 显示值的代码:public RLS_strid_panel(String namn1) { initComponents(); namn11_lbl.setText(namn1); }
  • 你应该添加你的堆栈跟踪。

标签: java methods get set


【解决方案1】:

要理解 get 和 set,这一切都与变量在不同类之间的传递方式有关。

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

设置值用于存储变量。

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

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

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

然后在我的Server类的run()方法中实现如下:

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

【讨论】:

    【解决方案2】:

    您的面板类没有接受字符串的构造函数

    尝试改变

    RLS_strid_panel p = new RLS_strid_panel(namn1);
    

    RLS_strid_panel p = new RLS_strid_panel();
    p.setName1(name1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      相关资源
      最近更新 更多