[英]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.