本文介绍了从Java中的其他类访问私有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我说的是我的话.我有一个这样的班级:

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

和其他类似的课程:

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

这是访问mainlabel并从其他类更改其值的真正方法(在此示例代码中,在SampleClass类中)是否有更好的解决方案?请注意,MainClass是具有main方法的类.

解决方案

从另一个类访问私有变量的正确方法是使用getter和setter方法.否则,您应该将该变量设为公开.

也就是说:

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

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

但是,直接返回私有数据是一种不好的做法-允许外部代码修改您的私有状态.通常,您应该返回私有数据的副本,以使外部代码不会与类的内部混乱.但是,如果您需要外部代码来调用私有数据上的方法,那么您应该在类中提供操作方法,而不是直接公开私有数据.

您可能真的想在主类中创建像setText()getText()这样的方法,然后在mainlabel上调用setText()getText()方法.但是,您需要注意这一点,因为您可能倾向于在类中复制JLabel定义的每个方法.这将使您的类及其使用者与JLabel实现紧密地结合在一起.如果您以后选择用其他方式替换JLabel,则需要花费大量工作来解开已创建的联轴器.

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?note that MainClass is class that has main method.

解决方案

The correct way to access a private variable from another class is with getter and setter methods. 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. However, you need to be careful with this, as you might be inclined to replicate every single method defined by JLabel in your class. That would tightly couple both your class and its consumers wit the JLabel implementation. 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.

这篇关于从Java中的其他类访问私有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:38