简体   繁体   English

为另一个类的Java对象提供内存

[英]Give memory to object from another class Java

I am a newbie to Java (I come from the C/C++ background) and I was having a hard time figuring out how to allocated memory of a data member in one class from another. 我是Java的新手(来自C / C ++背景),我很难弄清楚如何在一个类中分配数据成员的内存。 For eg, 例如

Class A 
{
    B bInA;
    C cInA;

    public void foo(someValue)
    {
         cInA = new C();
         cInA.foo(bInA, someValue)
    }

    public static void main(String args[])
    {
         A myA = new A();
         myA.foo(xyz)

         // myA.bInA.value should be equal to xyz
    }
}

Class B { ... }

Class C 
{
    public void foo(bInA, someValue)
    {
          bInA = new B();
          bInA.value = someValue;
    }
}

Can I do something like this in java? 我可以在Java中做类似的事情吗? Any help will be much appreciated. 任何帮助都感激不尽。

----EDIT----- - - 编辑 - - -

Class A 
{
    B bInA;
    C cInA;

    public void foo(someValue)
    {
         cInA = new C();
         bInA = new B();
         cInA.foo(bInA, someValue)
    }

    public static void main(String args[])
    {
         A myA = new A();
         myA.foo(xyz)

         // myA.bInA.value should be equal to xyz
    }
}

Class B { ... }

Class C 
{
    public void foo(bInA, someValue)
    {
          bInA.value = someValue;
    }
}

Java does not have pass-by-reference; Java没有传递引用。 rather, all you ever have are references to objects, and those references must be passed by value. 相反,您拥有的只是对对象的引用,并且这些引用必须按值传递。 So your code is roughly equivalent to something like this in C++: 因此,您的代码大致相当于C ++中的类似代码:

class A {
  private:
    B *bInA = NULL;
    C *cInA = NULL;

  public:
    void foo(someValue) {
      cInA->foo(bInA, someValue);
    }

    static void main() {
      A *myA = new A();
      myA->foo(xyz)

      // myA->bInA->value should be equal to xyz
    }
}

int main() {
    A::main();
    return 0;
}

class B { ... }

class C {
  public:
    void foo(bInA, someValue) {
      bInA = new B(); // defeats the point of having passed in a bInA
      bInA->value = someValue;
    }
}

(Except that the C++ code has memory leaks, since you allocate some things without freeing them, whereas in Java that's not an issue.) (除了C ++代码存在内存泄漏,因为您分配了一些东西而没有释放它们,而在Java中这不是问题。)

Unless I'm misunderstanding your intention (change value of bInA from C), your recent edit seems to work fine. 除非我误解了您的意图(从C更改bInA的值),否则您最近的编辑似乎正常。 Here's my java version of your pseudocode. 这是我的伪代码的Java版本。

class A 
{
    B bInA;
    C cInA;

    public void foo(int someValue)
    {
         cInA = new C();
         bInA = new B();
         cInA.foo(bInA, someValue);
         System.out.println(bInA.value);
    }

    public static void main(String args[])
    {
         A myA = new A();
         myA.foo(123);
         // myA.bInA.value should be equal to xyz
    }
}

class B { int value; }

class C 
{
    public void foo(B bInA, int someValue)
    {
          bInA.value = someValue;
    }
}

Output 产量

123

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM