Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:理解如何从其他类调用实例时遇到困难_Java_Instance - Fatal编程技术网

Java:理解如何从其他类调用实例时遇到困难

Java:理解如何从其他类调用实例时遇到困难,java,instance,Java,Instance,我一直在试图弄清楚如何从另一个类的类实例调用一个方法。例如: public class Test1 { public static void makeSomeInst() { Test1 inst = new Test1(); inst.fireTest(); } public void fireTest() { System.out.println("fireTest"); } p

我一直在试图弄清楚如何从另一个类的类实例调用一个方法。例如:

public class Test1 
 {

    public static void makeSomeInst()
    {
        Test1 inst = new Test1();
        inst.fireTest();
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }

    public Test1()
    {

    }

}
理解上面的内容没有问题,但是如果我想从一个名为Test2的类中执行inst,我该怎么做呢?下面的示例不起作用:

public class Test2 
{
    public static void main(String[] args) 
    {
        Test1.makeSomeInst();
        inst.fireTest();
    }
}
更清楚的是,我知道我可以调用静态引用而无需实例化,但我只想知道,在这种特定情况下


从Test2类引用名为inst的test1对象的语法是什么?

您必须将实例保存在某个地方。 如果Test1应为单例,则可以执行以下操作:

public class Test1 
{
    private static Test1 instance;

    public static Test1 getInstance()
    {
        return instance == null ? instance = new Test1() : instance;
    }

    public static void main(String[] args) 
    {
        Test1 inst = getInstance();
        inst.fireTest();
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }
}
在测试2中:

public class Test2 
{
    public static void main(String[] args) 
    {
        Test1.getInstance().fireTest();
    }
}
//编辑

正如我刚刚从@Thomas S.comment中学到的,单身并不是一个好的解决方案。
请参阅@candied_orange的答案以获得更好的实现。

您必须将实例保存在某个地方。 如果Test1应为单例,则可以执行以下操作:

public class Test1 
{
    private static Test1 instance;

    public static Test1 getInstance()
    {
        return instance == null ? instance = new Test1() : instance;
    }

    public static void main(String[] args) 
    {
        Test1 inst = getInstance();
        inst.fireTest();
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }
}
在测试2中:

public class Test2 
{
    public static void main(String[] args) 
    {
        Test1.getInstance().fireTest();
    }
}
//编辑

正如我刚刚从@Thomas S.comment中学到的,单身并不是一个好的解决方案。 请参阅@Candid_orange的答案以了解更好的实现

如果我想对一个名为Test2的类进行inst,我该怎么做呢

首先,你必须教
Test2
类什么是
Test1

public class Test2 
{
    public doSomething()
    {
        inst.fireTest();
    }

    public Test2(Test1 inst) 
    {
        this.inst = inst;
    }

    private Test1 inst;
}
public class Test1 
{

    public static void main(String[] args) 
    {
        Test1 inst = new Test1();

        Test2 inst2 = new Test2(inst); // <- new code

        inst2.doSomething();           // <- new code
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }

    public Test1()
    {

    }

}
然后教
inst2
对象什么是
inst

public class Test2 
{
    public doSomething()
    {
        inst.fireTest();
    }

    public Test2(Test1 inst) 
    {
        this.inst = inst;
    }

    private Test1 inst;
}
public class Test1 
{

    public static void main(String[] args) 
    {
        Test1 inst = new Test1();

        Test2 inst2 = new Test2(inst); // <- new code

        inst2.doSomething();           // <- new code
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }

    public Test1()
    {

    }

}
公共类Test1
{
公共静态void main(字符串[]args)
{
Test1 inst=新的Test1();
Test2 inst2=新的Test2(inst);//
如果我想对一个名为Test2的类进行inst,我该怎么做呢

首先,你必须教
Test2
类什么是
Test1

public class Test2 
{
    public doSomething()
    {
        inst.fireTest();
    }

    public Test2(Test1 inst) 
    {
        this.inst = inst;
    }

    private Test1 inst;
}
public class Test1 
{

    public static void main(String[] args) 
    {
        Test1 inst = new Test1();

        Test2 inst2 = new Test2(inst); // <- new code

        inst2.doSomething();           // <- new code
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }

    public Test1()
    {

    }

}
然后教
inst2
对象什么是
inst

public class Test2 
{
    public doSomething()
    {
        inst.fireTest();
    }

    public Test2(Test1 inst) 
    {
        this.inst = inst;
    }

    private Test1 inst;
}
public class Test1 
{

    public static void main(String[] args) 
    {
        Test1 inst = new Test1();

        Test2 inst2 = new Test2(inst); // <- new code

        inst2.doSomething();           // <- new code
    }

    public void fireTest()
    {
        System.out.println("fireTest");
    }

    public Test1()
    {

    }

}
公共类Test1
{
公共静态void main(字符串[]args)
{
Test1 inst=新的Test1();

Test2 inst2=new Test2(inst);//完全按照您之前所做的去做。@Joe C这很不清楚。我没有,您只做了您之前所做的一半。在您有两行之前。您的示例只有一行。就像@JoeC所说的,您忘记了
Test1 inst=new Test1()
在你的
Test2
类中。你是建议我创建一个新的Test1实例吗?我很难理解你的确切意思。我想问的是我如何引用一个已经存在的由另一个类创建的实例。完全按照你以前做的去做。@Joe C这很不清楚。我没有,你只做了一半在你有两行之前。你的例子只有一行。就像@JoeC说的,你忘记了
test1inst=newtest1()
在你的
Test2
类中。你是在建议我创建一个新的Test1实例吗?我很难理解你的确切意思。我想问的是我是如何引用一个已经存在的由不同类创建的实例的。非常感谢。我觉得我已经向这么多人问了这么多次这个问题,但是s是所有人实际制作的唯一有效示例。你太棒了!请不要建议单身。这是一个非常糟糕的模式。我对你的评论非常感兴趣。我不熟悉单身。感谢你就最佳实践提出建议。非常感谢。我觉得我已经向这么多人问了这么多次这个问题,但这是所有人实际制作的唯一有效示例。你太棒了!请不要建议单件。这是一个非常糟糕的模式。我对你的评论非常感兴趣。我不熟悉单件。谢谢你就最佳实践提出建议。