简体   繁体   English

从另一个类访问在主类中创建的对象方法

[英]Access an object method created in main class from another class

Is it possible to access an object created in main class? 是否可以访问在主类中创建的对象? or there is a way of get the owner/reference class? 还是有一种获取所有者/引用类的方法?

For example, in the code below, how can I call the method setMenu , that is on the myMainClass , from one of the Menu class objects ( firstMenu , secondMenu ) 例如,在下面的代码中,如何从一个Menu类对象( firstMenusecondMenu )中调用myMainClass上的setMenu方法。

I can make the Menu objects static but doesn't seems like the right approach... 我可以将Menu对象设为静态,但似乎不是正确的方法...

Main Class 主班

public class myMainClass extends JFrame {

    JPanel container;
    Menu firstMenu;
    Menu secondMenu;

    myMainClass() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);

        firstMenu = new Menu();
        secondMenu = new Menu();

        container = new JPanel(new CardLayout());
        container.add(firstMenu, "firstMenu");
        container.add(secondMenu, "secondMenu");

        add(container);
    }

    public void setMenu(String s) {
        CardLayout cl = (CardLayout) (container.getLayout());
        cl.show(container, s);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                myMainClass myMainClassObject = new myMainClass();
                myMainClassObject.setVisible(true);
            }
        });
    }
}

Menu Class 菜单类

public class Menu extends JFrame {

    Menu() {
        //How can I call myMainClass setMenu method from here?
        //myMainClass.setMenu("secondMenu");
        //myMainClassObject.setMenu("secondMenu");
    }
}

Thanks 谢谢

You've answered your own question. 您已经回答了自己的问题。 Make the setMenu method static. setMenu方法设为静态。

public void setMenu(String s) {
        CardLayout cl = (CardLayout) (container.getLayout());
        cl.show(container, s);
    }

And invoke it using the class name MyMainClass.setMenu in the Menu class. 并使用Menu类中的类名称MyMainClass.setMenu调用它。

Otherwise, you'll have to pass the instance of MyMainClass to the Menu class by overloading its constructor. 否则,您将必须通过重载MyMainClass的构造函数将其实例MyMainClassMenu类。

firstMenu = new Menu(this);
secondMenu = new Menu(this);

And in the other class, 在另一堂课中

public class Menu extends JFrame {

    Menu() {
        //How can I call myMainClass setMenu method from here?
        //myMainClass.setMenu("secondMenu");
        //myMainClassObject.setMenu("secondMenu");
    }

    Menu(MyMainClass main) {
        main.setMenu("secondMenu");
    }
}

setMenu() is an instance method if you want to call it you would need to instantiate myMainClass, which btw should be called MyMainClass, so you need to do: setMenu()是一个实例方法,如果要调用它,则需要实例化myMainClass,该实例应该被称为MyMainClass,因此您需要执行以下操作:

MyMainClass mainClass = new MyMainClass();
mainClass.setMenu("secondMenu");

If you want the method to be accessed via the class then you would need to create the method static, so it is a class method and not an instance method, so you can call it like: 如果要通过类访问该方法,则需要创建静态方法,因此它是一个类方法而不是实例方法,因此可以这样调用:

MyMainClass.setMenu("secondMenu");

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

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