|
Author |
Topic: communication between objects (Read 329 times) |
|
st33d
|
communication between objects
« on: Sep 27th, 2004, 10:13pm » |
|
I've recently put together my first object and now I'm trying to break down my code into more objects that I can utilise later. I'm trying to make a plotter and a recorder. The plotter asks the recorder to remember an expanding stack of co-ordinates. The plotter will also need to access the recorder's memory. My example of how I thought this should be expressed is below. Code: memory mem = new memory (100.0,100.0); foriegnObject obj = new foriegnObject (100.0,100.0); void loop() { if (mousePressed){ obj.inc(); obj.stack(); println(obj.x+" "+obj.y); } }//loop; class memory{ float [] xs=new float[1]; float [] ys=new float[1]; memory (float xtt,float ytt){ xs[0]=xtt; ys[0]=xtt; } void extendx(float newx) { float[] tempx = new float[xs.length + 1]; System.arraycopy(xs, 0, tempx, 0, xs.length); tempx[xs.length] = newx; xs = tempx; } void extendy(float newy) { float[] tempy = new float[ys.length + 1]; System.arraycopy(ys, 0, tempy, 0, ys.length); tempy[ys.length] = newy; ys = tempy; } }//memory; class foriegnObject { float x; float y; foriegnObject ( float xt, float yt ) { x=xt; y=yt; } void stack(){ mem.extendx(xs[mem.xs.length-1]); mem.extendy(ys[mem.ys.length-1]); mem.xs[mem.xs.length-1]=x; mem.ys[mem.ys.length-1]=y; }//stack; void inc(){ x++; y++; }//inc; }//foreignObject; |
| I've only tried to access an object from outside before, so I'm not sure if I've simply phrased this wrong or what. I'd appreciate it if anyone has a few moments to set me straight.
|
I could murder a pint.
|
|
|
fjen
|
Re: communication between objects
« Reply #1 on: Sep 27th, 2004, 10:33pm » |
|
Code: // <-- changes are marked like this memory mem = new memory (100.0,100.0); foriegnObject obj = new foriegnObject (100.0,100.0, mem); // <-- void loop() { if (mousePressed){ obj.inc(); obj.stack(); println(obj.x+" "+obj.y); } }//loop; class memory{ float [] xs=new float[1]; float [] ys=new float[1]; memory (float xtt,float ytt){ xs[0]=xtt; ys[0]=xtt; } void extendx(float newx) { float[] tempx = new float[xs.length + 1]; System.arraycopy(xs, 0, tempx, 0, xs.length); tempx[xs.length] = newx; xs = tempx; } void extendy(float newy) { float[] tempy = new float[ys.length + 1]; System.arraycopy(ys, 0, tempy, 0, ys.length); tempy[ys.length] = newy; ys = tempy; } }//memory; class foriegnObject { float x; float y; memory mem; // <-- // <-- foriegnObject ( float xt, float yt, memory m ) { x=xt; y=yt; mem = m; // <-- } void stack(){ mem.extendx(mem.xs[mem.xs.length-1]); // <-- mem.extendy(mem.ys[mem.ys.length-1]); // <-- mem.xs[mem.xs.length-1]=x; mem.ys[mem.ys.length-1]=y; }//stack; void inc(){ x++; y++; }//inc; }//foreignObject; |
| here you go ... an object (an instance of a class) does not know the variables that exsist out of it's scope (not inside it's { }) (see link below). to access variables on the outside you can pass a reference to another object to it (like i did with memory). this reference can then be used inside the class to access data on the outside. java variables (scroll down to "scope"): http://java.about.com/library/weekly/aa_vars1.htm http://www.particle.kth.se/~lindsey/JavaCourse/Book/Supplements/Chapter0 5/scope.html /F
|
« Last Edit: Sep 28th, 2004, 12:28am by fjen » |
|
|
|
|
fjen
|
Re: communication between objects
« Reply #2 on: Sep 28th, 2004, 12:30am » |
|
sorry my first explanation was somewhat confusing. .. hopefully it's a little better now .. /F
|
|
|
|
st33d
|
Re: communication between objects
« Reply #3 on: Sep 30th, 2004, 11:14pm » |
|
Thanks. I knew about the hermetic nature of objects but I hadn't actually had to deal with it what with using just the one object. I think I've just got dodgy equations and poor typing to deal with now.
|
I could murder a pint.
|
|
|
|