访问一个没有实例的 “instance”?

huangapple go评论75阅读模式
英文:

Accessing an "instance" without an instance?

问题

这是我通常的做法:

package framecanvasagain;

public class Framecanvasagain {

    public MyFrame theframe;

    public static void main(String[] args) {
        //正常创建框架
        theframe = new MyFrame();

        //现在假设我以后想再次访问该框架
        theframe.setLocationRelativeTo(null);
    }
    
}

我可以创建一个JFrame而不创建它的实例,但以后有没有办法访问那个JFrame

package framecanvasagain;

public class Framecanvasagain {

    public static void main(String[] args) {
        new MyFrame(); //Netbeans显示“忽略新实例”,但仍然加载了一个JFrame

        //现在假设我以后想再次访问该框架
        MyFrame().setLocationRelativeTo(null); //这会返回一个错误,“找不到符号”
    }
    
}
英文:

So, I'm thinking of creating a JFrame without making an instance of it.

This is how I normally do it:

package framecanvasagain;

public class Framecanvasagain {

    public MyFrame theframe;

    public static void main(String[] args) {
        //creates the frame normally
        theframe = new MyFrame();

        //now let's say I want to access the frame again later on
        theframe.setLocationRelativeTo(null);
    }
    
}

I could create a JFrame without creating an instance of it, but is there a way to access that JFrame later on?

package framecanvasagain;

public class Framecanvasagain {

    public static void main(String[] args) {
        new MyFrame(); //Netbeans says "New Instance Ignored" but it still loads a JFrame

        //now let's say I want to access the frame again later on
        MyFrame().setLocationRelativeTo(null); //this returns an error, "cannot find symbol"
    }
    
}

答案1

得分: 1

答案很简单,不可以。您需要将其赋值给一个变量,以便再次访问其属性。

new MyFrame();

创建了一个 JFrame 的实例,但是 Java 垃圾收集器会立即将其回收,因为它没有被赋值给任何东西。

您可以这样做:

(new MyFrame()).setLocationRelativeTo(null);

然而,您仍然会创建一个实例,无法再次访问其属性。在 Java 中,没有办法在不创建对象并将其存储的情况下重用对象。这是面向对象编程语言的困境。

将其制作成单例模式可能是一种替代方法。

https://www.geeksforgeeks.org/singleton-class-java/

尽管如此,我认为这可能不是您在这里尝试做的事情。

英文:

The simple answer is no. You need to set it to a variable in order to access it's properties again.

 new MyFrame(); 

creates an instance of the JFrame but the Java garbage collector picks it up right away since it isn't set to anything.

you could do this :

(new MyFrame()).setLocationRelativeTo(null);

However, you would still be creating an instance of it and wouldn't be able to access its properties again. There is no way to reuse an object without creating it and then storing it in Java. The perils of an object orientated programming language.

Making it into a singleton might be an alternative.

https://www.geeksforgeeks.org/singleton-class-java/

Although, I don't think that's really what you are trying to do here.

答案2

得分: 1

你的问题标题不正确:

你实际上正在询问的是:
我能在没有引用的情况下访问一个实例吗?

而答案是:不行!

在你的示例代码中,你确实创建了一个JFrame的实例,但你立即丢弃了对它的引用,因此无法访问该特定实例。

此外,它随时可能被垃圾回收(除非库的某个内部部分也持有对它的引用,我不是AWT内部的专家),因此你的窗口可能会随机消失。

但真正的问题是:你在尝试实现什么?

英文:

Your question title is incorrect:

What you are actually asking is:
Can I access an instance without a reference to it?

And the answer is: no, you can not!

In your example code you do create an instance of JFrame but you immediately discard the reference to it, so you have no way of addressing that particular instance.

Also, it may be garbage collected at any time (unless some internal part of the library is also holding a reference to it, I am not an expert in AWT internals), so your frame could disappear at random.

But the real question is: what are you trying to achieve here?

答案3

得分: 0

你只需添加一个单词static,就可以实现你所要求的功能。将你的myFrame变量设为static。以下是稍作修改的第一个示例。我将“do later”调用移到另一个方法中,以强调你的MyFrame可以从任何地方访问:

package framecanvasagain;

public class Framecanvasagain {

    public static MyFrame theframe;

    public void someOtherMethod() {
        //现在假设我稍后想要再次访问这个frame
        Framecanvasagain.theframe.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        //正常创建frame
        theframe = new MyFrame();

        //....
    }
}
英文:

You can do what you're asking by just adding a single word...static. Just make your myFrame variable static. Here's your first example, modified a bit. I moved the "do later" call into another method just to stress the point that your MyFrame can be accessed from anywhere:

package framecanvasagain;

public class Framecanvasagain {

    public static MyFrame theframe;

    public void someOtherMethod() {
        //now let's say I want to access the frame again later on
        Framecanvasagain.theframe.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        //creates the frame normally
        theframe = new MyFrame();

        ....

    }
}

huangapple
  • 本文由 发表于 2020年10月4日 06:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64189568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定