简体   繁体   English

由于java没有引用,因此如何在两个单独的类之间传递信息?

[英]As java doesn't have referencing, how does one pass information between two separate classes?

Sorry for the extremely vague/confusing, I really didn't know how to name this issue. 非常抱歉,我确实不知道如何命名此问题。 If someone has a better one please feel free too edit it. 如果有人有更好的选择,请随时编辑。 Onto my issue, 关于我的问题

public class Foo()
{
    String name;
    public Foo()
    {
        Bar temp = new Bar();
    }
}    

public class Bar()
{
    public Bar()
    {
        setFooName("newName");
    }

    public void setFooName(String name)
    {   
        // Is it possible call a method in Foo?
    }
}    


public class test {
    static public void main(String[] args) 
    {
        Foo foobar = new Foo();
    }
}    

I'm still new to java so I'm not sure if this is even possible, but using C++ I'd normally have a local variable of the 'Foo' inside the 'Boo' class and then pass a reference to the object into the constructor of Bar and then assign it to the local variable in Bar. 我对Java还是很陌生,所以我不确定这是否可能,但是使用C ++时,通常会在'Boo'类中使用'Foo'的局部变量,然后将对该对象的引用传递给Bar的构造函数,然后将其分配给Bar中的局部变量。

As java doesn't have referencing I'm stuck on the matter. 由于java没有引用,因此我对此事感到困惑。 The reason I want to do this is the example I have is I need to create a GUI object inside of a class and then have information from the GUI object sent back to the class it was created in. 我要这样做的原因是我拥有的示例是我需要在类内部创建一个GUI对象,然后将来自GUI对象的信息发送回创建它的类中。

I do hope this all makes sense, if it doesn't, sorry. 我确实希望一切都有意义,如果没有,对不起。

I think you're getting caught up on symantics. 我认为您正在陷入困境。

The following example passes a copy of the reference of Foo to Bar via Bar 's constructor. 以下示例通过Bar的构造函数将Foo引用的副本传递给Bar This now allows Bar to access properties of the instance of Foo . 现在,这使Bar可以访问Foo实例的属性。 What it doesn't allow you to do is change the reference between the two (ie assigning a new reference of Foo within bar will not change the previous reference) 它不允许您做的是在两个引用之间进行更改(即在bar中分配新的Foo引用不会更改先前的引用)

public class Foo()
{
    String name;
    public Foo()
    {
        Bar temp = new Bar(this);
    }
}    

public class Bar()
{
    private Foo foo;
    public Bar(Foo foo)
    {
        this.foo = foo;
        setFooName("newName");
    }

    public void setFooName(String name)
    {   
        // Is it possible call a method in Foo?
    }
}    


public class test {
    static public void main(String[] args) 
    {
        Foo foobar = new Foo();
    }
}  

Bar could then call any modifiers that Foo provides and it would change those individual properties of Foo for everybody who had a reference to that instance... 然后Bar可以调用Foo提供的任何修饰符,并且它将为每个引用该实例的人更改Foo那些单独属性...

I hope that's close to what you're asking :P 我希望这接近您的要求:P

Actually you can do this in Java. 实际上,您可以使用Java执行此操作。 Java has references, but not pointers. Java有引用,但没有指针。 Confusingly, for C++ aficionados, these references can be null. 令人困惑的是,对于C ++爱好者,这些引用可以为null。

This means you can certainly do the following: 这意味着您当然可以执行以下操作:

public class Bar {
    private Foo foo;
    public Bar(Foo foo) {
        this.foo = foo;
    }
    public setFooName(String name) {
        foo.setName(name);
    }
}
public class Foo {
    private String name;
    public Foo() {
        Bar temp = new Bar(this);
        temp.setFooName("fooname");
    }
}

But I'm not sure, in this instance, why you wouldn't just write: 但在这种情况下,我不确定为什么您不会只写:

public class Foo {
    private String name;
    public Foo() {
        this.name = "fooname";
    }
}

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

相关问题 如何使用方法在类之间传递信息 - How to use methods to pass information between classes 在Java中的两个类之间传递变量值 - Pass variable value between two classes in java 如何在Java中的不同类之间共享数据 - How to share data between separate classes in Java 如何在JAVA中一次传递一个类的两个单独实例来对该类中的某些数据进行排序 - How to pass two separate instances of a class at a time in JAVA to sort some data in that classes Java-在单独的类中的两个变量之间创建一致性 - Java- Creating Consistency Between Two Variable in Separate Classes Java JDialogs如何在两者之间传递信息? - Java JDialogs How To Pass Information Between? 如何在类之间传递Java中的LinkedList对象 - How to pass LinkedList object in java between classes 如何在多个类之间传递对象? Java的 - How to pass an object between multiple classes? Java 我有以下两种不同的方式在 Java 中声明一个 integer 数组,一种有效,一种无效,为什么? - I have the following two diffetent ways of declaring an integer array in Java, one works and one doesn't, why? 无法在Eclipse上使用Java在两个类之间传递Selenium WebDrivers - Can not Pass Selenium WebDrivers between two classes using Java on Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM