 |
Author |
Topic: Arrays of objects (and extended objects) (Read 570 times) |
|
james-wddg
|
Arrays of objects (and extended objects)
« on: Jan 5th, 2005, 9:10pm » |
|
Can someone help me out on a way to do the following. I have a class: class Agent it is extended as class Commnicator I want to be able to use a single multidimensional array of these objects that doesn't care whether an object at a specific point in the array is the base Agent class or the extended class Communicator. that's about it - any insight would be great.
|
|
|
|
amoeba
|
Re: Arrays of objects (and extended objects)
« Reply #1 on: Jan 6th, 2005, 12:56am » |
|
ok, given: Code:class Communicator { } class Agent extends Communicator { } // then you can do the following: Communicator c[]=new Communicator[100]; c[0]=new Communicator(); c[1]=new Agent(); // this only works if c[1] really is an Agent object. Agent a=(Agent)c[1]; |
| you need to typecast to the subclass to get access to fields and methods not defined in the superclass. get the idea?
|
marius watz // amoeba http://processing.unlekker.net/
|
|
|
james-wddg
|
Re: Arrays of objects (and extended objects)
« Reply #2 on: Jan 6th, 2005, 4:58pm » |
|
Is there any way to tell what class an object belongs to without keeping a list somewhere?
|
|
|
|
st33d

|
Re: Arrays of objects (and extended objects)
« Reply #3 on: Jan 6th, 2005, 6:27pm » |
|
Most datatypes in java have a method that will alter it or find out out any relevant information you want. Code: class1 c1 = new class1(); class2 c2 = new class2(); void setup(){ } void draw(){ println(c1.getClass()); println(c2.getClass()); } class class1 { } class class2 extends class1 { } |
| It's worth looking through the link below for methods you can apply to Strings, ints, floats and so on for useful tools before you go off writing functions to get around problems (as I was starting to). All of it should work in Processing. http://java.sun.com/j2se/1.3/docs/api/index.html I hope this helps.
|
I could murder a pint.
|
|
|
liminal
|
Re: Arrays of objects (and extended objects)
« Reply #4 on: Jan 6th, 2005, 6:32pm » |
|
There are a few ways to tell what type an object is. For what you're doing, the instanceof operator is probably what you want: class Communicator {} class Agent extends Communicator {} Communicator[] array; if (array[i] instanceof Agent) { // sub class } else { // must be the superclass } There are other things you can do too, such as reflection, but don't go there unless you need to.
|
|
|
|
liminal
|
Re: Arrays of objects (and extended objects)
« Reply #5 on: Jan 6th, 2005, 6:36pm » |
|
Ok, s33d 'went there' so if you want to use reflection, then you'd do something like this: if (array[i].getClass() == Agent.class) { } else { }
|
|
|
|
TomC
|
Re: Arrays of objects (and extended objects)
« Reply #6 on: Jan 6th, 2005, 6:44pm » |
|
on Jan 6th, 2005, 4:58pm, james-wddg wrote:Is there any way to tell what class an object belongs to without keeping a list somewhere |
| If you need to know what type an object is, you probably shouldn't put it in a mixed-type collection. The point of this is that if you have a bunch of different objects which are all inherited from a particular base class, if you put them in one big collection you can treat them all the same way, even if they implement different functionality. e.g. for a bunch of things which can be drawn... Code: class Drawable { void draw() { // do nothing // NB:- Drawable should really be an interface but that's another lesson } } class Rectangle extends Drawable { void draw() { rect(0,0,10,10); } } class Circle extends Drawable { void draw() { ellipse(0,0,10,10); } } Drawable[] drawables; void setup() { size(100,100); drawables = new Drawable[10]; for (int i = 0; i < drawables.length; i++) { // roughly half the time, make a circle, otherwise make a rectangle if (random(0.0,1.0) < 0.5) { drawables[i] = new Circle(); } else { drawables[i] = new Rectangle(); } } } void draw() { for (int i = 0; i < drawables.length; i++) { // we only care that it has a draw method // the right one will be called automatically drawables[i].draw(); // move the origin for the next one translate(width/drawables.length, height/drawables.length,0); } } |
|
|
« Last Edit: Jan 6th, 2005, 6:45pm by TomC » |
|
|
|
|
james-wddg
|
Re: Arrays of objects (and extended objects)
« Reply #7 on: Jan 6th, 2005, 8:14pm » |
|
TomC - thanks, that was orginally what I was trying to do. I honestly don't know how I ended up rewriting everything with the Vector class. question: the compiler dies on me when I try to do this: class Agent{ Agent(int something){ int somethinghere = 1; } } class SuperAgent extends Agent{ SuperAgent(){ } } can you extend classes with constructors that have arguments passed into them? thanks
|
|
|
|
TomC
|
Re: Arrays of objects (and extended objects)
« Reply #8 on: Jan 7th, 2005, 1:02pm » |
|
The SuperAgent constructor is trying to call an Agent constructor first, and expects one with no arguments. You'll either need to implement a constructor for Agent which doesn't need any arguments, or explicitly call the Agent(int something); constructor from the SuperAgent constructor using the special super() method. Remember you can have as many constructors as you like, so long as they take different arguments. It's good practice to implement a no-argument constructor which assigns default values to all the base class attributes so that you don't have to worry about setting up the base class from your subclasses. So either: Code: class Agent { // ... Agent() { // default initialisation in here } } |
| or: Code: class SuperAgent extends Agent { // ... SuperAgent() { super(0); // set up the Agent part of SuperAgent // SuperAgent-specific initialisation here } } |
| The super() function is a special function that calls the constructor of the base class. It can take arguments, so long as there is a corresponding constructor. So if SuperAgent extends Agent, then calling super(0) will use the Agent(int something) constructor. This example might be clearer, without the Super/super() confusion: Code: class Vehicle { int numwheels; Vehicle() { numwheels = 4; println("made a vehicle with " + numwheels + " wheels"); } Vehicle(int numwheels) { this.numwheels = numwheels; println("made a vehicle with " + numwheels + " wheels"); } } class Car extends Vehicle { int numdoors; Car() { super(); numdoors = 3; println("made a car with " + numdoors + " doors"); } Car(int numdoors) { super(4); this.numdoors = numdoors; println("made a car with " + numdoors + " doors"); } Car(int numwheels, int numdoors) { super(numwheels); this.numdoors = numdoors; println("made a car with " + numdoors + " doors"); } } println("v1:"); Vehicle v1 = new Vehicle(); println("v2:"); Vehicle v2 = new Vehicle(2); println("c1:"); Car c1 = new Car(); println("c2:"); Car c2 = new Car(2); println("c3:"); Car c3 = new Car(5,7); |
| Will output this: v1: made a vehicle with 4 wheels v2: made a vehicle with 2 wheels c1: made a vehicle with 4 wheels made a car with 3 doors c2: made a vehicle with 4 wheels made a car with 2 doors c3: made a vehicle with 5 wheels made a car with 7 doors Try changing the values in the Vehicle constructors, and commenting out the calls to super in the Car constructors. That should help you figure out what happens.
|
« Last Edit: Jan 7th, 2005, 1:09pm by TomC » |
|
|
|
|
james-wddg
|
Re: Arrays of objects (and extended objects)
« Reply #9 on: Jan 7th, 2005, 5:18pm » |
|
That's pretty cool. I ended up essentially doing what you said. I didn't know that you can have multiple contructors for a class tho. I created the basic Agent(){} functions then created another init(){} function that I would pass all the variables into. Probably not the best coding methodology - but it got me moving again. thanks! James
|
|
|
|
toxi
|
Re: Arrays of objects (and extended objects)
« Reply #10 on: Jan 7th, 2005, 5:58pm » |
|
having worked on a few bigger java projects recently, i came to prefer interfaces over using inheritance. code becomes a lot cleaner whilst providing more flexibility than extending classes. interfaces are java's answer to multiple inheritance: a class can "implement" more than one interface. also, by cleverly defining what features go in an interface you can also interpret them as different "views" or axes to your code/class. e.g. a v.similar example outside the realm of coding could be the del.icio.us social bookmarks manager, which allows you to access data via 3 axes: users, tags, urls. bringing it back to a coding point of view, each del.icio.us entry "implements" these 3 interfaces. more info in this thread and these articles: Why Java Interfaces Are So VERY Cool Inheritance versus composition: Which one should you choose
|
http://toxi.co.uk/
|
|
|
amoeba
|
Re: Arrays of objects (and extended objects)
« Reply #11 on: Jan 8th, 2005, 1:34am » |
|
on Jan 7th, 2005, 5:58pm, toxi wrote:having worked on a few bigger java projects recently, i came to prefer interfaces over using inheritance. code becomes a lot cleaner... |
| toxi, ever the purist. you're right, of course, except that using true inheritance allows you to reuse code. personally I tend to use a strategy like TomC's "Drawable" example. it allows tailoring individual subclasses while keeping core code common, provided the central functionality can be provided in a generic way , i.e. draw() etc... interfaces are nice, tho.
|
marius watz // amoeba http://processing.unlekker.net/
|
|
|
toxi
|
Re: Arrays of objects (and extended objects)
« Reply #12 on: Jan 8th, 2005, 1:36pm » |
|
on Jan 8th, 2005, 1:34am, amoeba wrote: toxi, ever the purist. you're right, of course, except that using true inheritance allows you to reuse code. |
| i guess it has more to do with my belief that Occam's Razor also applies to our thoughts and ideas expressed as code... also didn't mean to say using interfaces is the best at all occasions. inheritance has its place of course too! btw. remember, i'm a jack of all trades, not a purist!
|
http://toxi.co.uk/
|
|
|
|