简体   繁体   English

从Java中的其他类访问私有变量

[英]access private variable from other class in java

i hope that i mean my words. 我希望我说的是我的话。 i have a class like this: 我有这样的课:

public class MainClass extends JFrame{
    private JLabel mainlabel;
    private SampleClass sample=new SampleCalss();

    public void intital(){
        mainlabel=new JLabel("Main");
        sample.setMethod(getLabel());
        //
        //some code
        //
        add(mainlabel); 
    }

    public static void main(){
        intital();
    }

    public JLabel getLabel(){
        return mainlabel;
    }
}

and other class like this: 和其他这样的类:

public class SampleClass extends JFrame{
    private JButton button=new JButton("Change");
    private JLabel sLabel;

    public SampleClass(){
        //somecode
        //
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                sLabel.setText("Sample text set");
            }
        });
        add(jButton);
    }

    public void setMethod(JLabbel l){
        sLabel=l;
    }
}

is this a true way to access mainlabel and change its value from other class(in this sample code in class SampleClass ) is there a better or right solution? 这是访问mainlabel并从其他类更改其值的正确方法(在SampleClass类的此示例代码中)是否有更好的解决方案? note that MainClass is class that has main method. 请注意, MainClass是具有main方法的类。

The correct way to access a private variable from another class is with getter and setter methods. 从另一个类访问私有变量的正确方法是使用getter和setter方法。 Otherwise, you should have made that variable public. 否则,您应该将该变量公开。

That is: 那是:

// getter
public JLabel getMainLabel() { 
    return mainlabel;
}

// setter
public void setMainLabel(JLabel mainLabel) {
    this.mainlabel = mainLabel;
}

However, it is a bad practice to return private data directly - that allows external code to modify your private state. 但是,直接返回私有数据是一种不好的做法-允许外部代码修改您的私有状态。 In general, you should return a copy of your private data so that external code can't mess with the internals of your class. 通常,您应该返回私有数据的副本,以使外部代码不会与类的内部混乱。 But if you need external code to call methods on your private data, then you should probably be providing manipulation methods in your class, rather than directly exposing private data. 但是,如果您需要外部代码来调用私有数据上的方法,那么您可能应该在类中提供操作方法,而不是直接公开私有数据。

You probably really want to create methods like setText() and getText() in your main class, and then call the setText() and getText() methods on mainlabel . 您可能真的想在主类中创建setText()getText()类的方法,然后在mainlabel上调用setText()getText()方法。 However, you need to be careful with this, as you might be inclined to replicate every single method defined by JLabel in your class. 但是,您需要注意这一点,因为您可能倾向于在类中复制JLabel定义的每个方法。 That would tightly couple both your class and its consumers wit the JLabel implementation. 这将使您的类及其使用者与JLabel实现紧密地结合在一起。 If you choose to replace the JLabel with something else in the future, it will take a lot of work to unwind the coupling you've created. 如果您将来选择用其他方式替换JLabel ,则需要花费大量工作来解开已创建的耦合。

Create a mutator method that will allow access to the private variable 创建一个mutator方法,该方法将允许访问私有变量

public void setLabel(JLabel _label) {

mainlabel = _label;
}

You can use Java Reflection to access the private variables and change the values. 您可以使用Java Reflection访问私有变量并更改值。

http://download.oracle.com/javase/tutorial/reflect/member/fieldValues.html http://download.oracle.com/javase/tutorial/reflect/member/fieldValues.html

Why not expose the mainlabel through setMainLabel ? 为什么不通过setMainLabel公开mainlabel?

public void setMainLabel(JLabel mainLabel) {
    this.mainlabel = mainLabel;
}

In class, private variables can be used without explicitly calling getter method for private variable. 在类中,可以使用私有变量,而无需显式调用私有变量的getter方法。

So it would be like below for the first class 所以就像下面的第一节课

sample.setMethod(mainlabel);

And your assumption for setting private variable in different class by using setter method is correct. 您使用setter方法在不同类中设置私有变量的假设是正确的。

The problem with your method is if MainClass ever changes its JLabel (by creating a new one), SampleClass still has a reference to the old one. 您的方法存在的问题是,如果MainClass更改了JLabel(通过创建新的JLabel),SampleClass仍然具有对旧JLabel的引用。 You can give SampleClass a reference to MainClass via the constructor or a setter method: 您可以通过构造函数或setter方法为SampleClass提供对MainClass的引用:

public class SampleClass extends JFrame{
private JButton button=new JButton("Change");
private MainClass main;

public SampleClass(MainClass main)
{
    this.main = main;
    // do stuff
}

And whenever you need to access MainClass' label, call its getter method: 并且每当您需要访问MainClass的标签时,请调用其getter方法:

main.getLabel();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM