java 从另一个类访问在主类中创建的对象方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5505257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access an object method created in main class from another class
提问by Rias
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 Menuclass objects (firstMenu, secondMenu)
例如,在下面的代码中,如何从Menu类对象之一(firstMenu、secondMenu)调用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
谢谢
采纳答案by adarshr
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.
并使用类MyMainClass.setMenu
中的Menu
类名调用它。
Otherwise, you'll have to pass the instance of MyMainClass
to the Menu
class by overloading its constructor.
否则,您必须通过重载其构造函数将 的实例传递MyMainClass
给Menu
类。
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");
}
}
回答by Maricel
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");