Sending messages to other object's instances?

Joined
Mar 2, 2009
Messages
5
Reaction score
0
Points
1
*let me preface this question with "noob".

Say we have 3 objects: controller , box1, and box2.

"box1" and "box2" are instances created in "controller" and both have a method called "crunch".

Calling "crunch" from "controller" is no problem, but how do I call "box2"s crunch method from "box1"?

Said another way:

If an object instantiates 2 or more new objects, how can those new objects send messages to each other?

Thanks
 
Joined
Feb 15, 2009
Messages
6
Reaction score
0
Points
1
Create a reference to the box class in the box class and then reference the box 2 to your box1.
 
OP
N
Joined
Mar 2, 2009
Messages
5
Reaction score
0
Points
1
Shogun: Thanks for the reply. Trying to digest your suggestion. Not sure exactly how to implement it.

*scouring source material*
 
Joined
Feb 15, 2009
Messages
6
Reaction score
0
Points
1
don't know what language you use but in Java you would implement it something along the lines of this:

writing these straight here, may be a few typos.
box class:
Code:
public class Box {
    private Box boxWithInBox; //Imagine that we keep boxes within boxes

    public Box (){
    }

    public void crunch (){
        //do stuff
    }

    public void crunchBoxWithInBox(){
        getBoxWithInBox ().crunch();
    }

    public void set boxWithInBox (Box boxWithInBox){
        this.boxWithInBox = boxWithInBox;
    }

    public Box getBoxWithInBox (){
        return boxWithInBox;
    }
}

controller class:
Code:
public Class Controller {
    
    public Controller (){
    }

    public void doStuff (){
        Box box1 = new Box ();
        Box box2 = new Box ();

        box1.setBoxWithInBox (box2);

        box1.crunchBoxWithInBox ();
    }
}
 
OP
N
Joined
Mar 2, 2009
Messages
5
Reaction score
0
Points
1
Shogun: Thanks for the reply. I am using objective C but I understand your point. I will give it a try in the morning.

Thanks for taking the time.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top