Skip to main content
gamedev.net
🔒 Locked

[java] Java OO question..

Started by ArchangelMorph 10 replies 1.4k views
Original Post
ArchangelMorph
ArchangelMorph
I've come alittle stuck in my project and i'm pretty sure the problem is down to the way i'm doing my OO.. Ok here's the problem: I have Obj A which is an instance of Class A and Obj B which is an instance of Class B.. When Obj B is created its constructor creates Obj C (which is an instance of Class C).. Now how do I make Obj C access the Vars and Methods of Obj A? (This might very well be a very noob-ish question but i'm a little rusty so please bare with me..)
Wan
Wan
If I understand correctly, this is the situation:
class A{}class B{  private C c;  B()  {    c = new C();  }}class C{}

Quote:
Now how do I make Obj C access the Vars and Methods of Obj A?

Do you mean
a) C should inherit A and use it methods?
b) C should be able to refer to an instance of A and call it methods?
ToohrVyk
ToohrVyk
Yes, your object of class C requires an object of type A, which must be given to it by B during its creation.

So B requires an object of class A during its creation.

ArchangelMorph
ArchangelMorph
Quote:
Original post by ToohrVyk
Yes, your object of class C requires an object of type A, which must be given to it by B during its creation.

So B requires an object of class A during its creation.


I tried this but my applet wouldn't instanciate (Obj B) when I passed the object to the constructor..
ToohrVyk
ToohrVyk
class A {   public int i = 0;}class C {  private A a;  public C( A passed ) {    a = passed;  }  public void foo( ) {    a.i = 18;  }}class B {   private C c;  public B( A a ) {    c = new C(a);  }  public void foo( ) {    c.foo( );  }}class Main {  public static void main( string [] args ) {    A a = new A( );    B b = new B(a);    b.foo( );    System.out.println( "This value should be 18: a.i = " + a.i );  }}
ArchangelMorph
ArchangelMorph
Quote:

public void foo( ) {
c.foo( );
}


Noob question, what does this code do? (I think this is where I went wrong)
ToohrVyk
ToohrVyk
This is a member function of B. It says that when calling b.foo( ), it will call b.c.foo( ).
ArchangelMorph
ArchangelMorph
Quote:
Original post by ToohrVyk
This is a member function of B. It says that when calling b.foo( ), it will call b.c.foo( ).


Yeah but what does calling b.c.foo() or b.foo() actually do?
Son of Cain
Son of Cain
Getter and Setter?

class A {  private int value;  A (int a) {    value = a;  }    public int getAValue() { return a; }}class B {  B(A a) {    new C(a);  }}class C {  C(A a) {    System.out.println("Value of \"a\": " + a.getAValue());  }}


Ugly code, but you may understand what I mean =P
a.k.a javabeats at yahoo.ca
ArchangelMorph
ArchangelMorph
Quote:
Original post by Son of Cain
Getter and Setter?

class A {  private int value;  A (int a) {    value = a;  }    public int getAValue() { return a; }}class B {  B(A a) {    new C(a);  }}class C {  C(A a) {    System.out.println("Value of \"a\": " + a.getAValue());  }}


Ugly code, but you may understand what I mean =P


Yeah I think I tried something similar to this but it didn't work for some reason..

Ok let me try to elaborate on my problem:

I'm using Java3D to create a 3D scene in an applet,

My primiary class which extends applet and creates the scene graph is called Final3D..

The problem is Final3D creates Rotator3D and Animator3D Obj's and the Rotator3D Obj is fine..

But the Animator3D Obj must access the methods and data of the dData3D Obj (which is created in the main() method before the Final3D Obj is instanciated)..

Now I tried to pass the dData Obj to the constructor of the Final3D Obj (so it could then be passed to the Aniamtor3D Obj when it is created) but it won't let me do this.. Does this have something to do with the fact that the Final3D Obj extends applet or am I having different problems?

(Sorry another noob Q but i'm rusty..: Also whats a static method and var? and how/when would they be used typically?)
Son of Cain
Son of Cain
Post some code, it will be easier to help out. =D

If I remember correctly, a static method is one that doesn't have to be called by an instance of a class. I don't know the textbook answer, but its like the following:

public class ActiveObjectFactory {    public static final ActiveObject load(String path) {}}// Somewhere...ActiveObject aobj = ActiveObjectFactory.load("resources/xml/MyData.xml");


Static context can't access non-static variables. You need to declare these variables static as well, in order to use them in static contexts (i.e. method).
a.k.a javabeats at yahoo.ca

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.