【问题标题】:Accessing objects made in main method from other classes从其他类访问在 main 方法中创建的对象
【发布时间】:2011-12-23 14:45:06
【问题描述】:

嘿,我正在尝试从另一个类访问我在 main 方法中创建的对象,但是当我在另一个类中引用它时,它无法识别。经过一些研究,我认为这与访问修饰符有关,但我尝试公开对象只是为了显示“删除无效修饰符”的评论。任何指针?

很抱歉,这太基础了,但我只是一个初学者,我发现这东西很难。

抱歉没提!我正在用 Java 编写。这就是我所拥有的:

public static void main(String[] args) {

    Mainframe mainframe = new Mainframe();
    mainframe.initialiseMF();       
    LoginPanel lp = new LoginPanel();
    mainframe.add(lp);
}

public class Mainframe extends JFrame {

public Mainframe () {
    // Set size of mainframe
    setBounds(0, 0, 500, 500);

}

public void initialiseMF (){
    // Get the size of the screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the mainframe
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the mainframe
    setLocation(x, y);
    setVisible(true);
}

}

我正在尝试在另一个类中执行此语句:

Container content = mainframe.getContentPane();   

【问题讨论】:

  • 你用什么语言写作?能举个简单的例子吗?
  • 请发布一些代码,否则每个人都只是在猜测。
  • 你能发布更多其他类的代码吗?您打算在其他类中使用大型机的内容窗格做什么?如果您想在 UI 中添加一些内容,请在 Mainframe 中进行。

标签: java access-modifiers


【解决方案1】:

请记住,大型机对象是静态的 main() 方法的本地对象。您无法在课堂外访问它。

可能这样会更干净一些。

public class Mainframe extends JFrame{
     public Mainframe(){
          initialiseMF ();
     }

     public void initialiseMF (){
          //do ur inits here
     }

}

然后这样做,

public class TheOtherClass{

    private Mainframe mainFrame;

    public TheOtherClass(){
        mainFrame = MainFrame.mainFrame; //although I would not suggest this, it will avoid the Main.mainFrame call
    }

    public void otherMethodFromOtherClass(JFrame mainFrame){
        Container content = mainFrame.getConentPane();
    }
}

【讨论】:

  • 感谢您的帮助!我正在尝试通过从空白框架(大型机)开始在我的程序屏幕之间切换,并根据需要添加/删除面板以显示不同的屏幕。我通过扩展 JFrame 类创建了一个 Mainframe 类,并编写了一些代码使其在屏幕上居中。我想在我创建的大型机上添加一个面板。啊,我似乎已经通过将其更改为 Main.mainframe.getContentPane() 来修复它 - 有什么办法可以避免引用 Main 吗?
  • @user1058210 :可以,请参阅更新后的答案。您可以将变量作为成员保存在 TheOtherClass
【解决方案2】:

我不确定你所说的“对象”是什么意思,但我只是猜测你的意思是变量。

为了使在类中声明的变量可以从外部访问(以某种方式,即直接或通过某种方法),它必须是成员变量而不是局部变量。

例如局部(方法局部)变量:

//local variables are the ones that are declared inside a method
//their life and visibility is limited only within the block in which they are define.
public static void main(String[] args) { // args is also a local variable
    String localVar = "Access modifiers are not allowed for local variables.";
    //the reason that the access modifiers are not allowed is because
    //the local variables are not members of the class        
}

只有将类的成员设置为 privateprotected、package-private 或 public 才有意义。

例如成员变量:

class AClass {
    private String memberVar = "A member variable can have access modifier.";
    //the access modifier will determine the visibility of that variable.

    public String getMemberVar() {
        return this.memberVar;
    }
} 
  1. private:仅在班级内可见。您可以使用一些公共方法使其可访问。通常称为 getter 方法。

  2. package-private:仅在包内可见。同样,您可以使用一些公共方法使其在包外访问。

  3. 受保护:仅在类及其子类中可见(即使子类在包外也无所谓)。

  4. 公开:随处可见。

【讨论】:

  • 我需要再次阅读问题。从那以后,它已经更新了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 2017-04-22
相关资源
最近更新 更多