FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   classes and subclasses
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: classes and subclasses  (Read 187 times)
rgovostes

rgovostes
classes and subclasses
« on: Dec 21st, 2003, 1:02am »

I am working on a Proce55ing implementation of Mars Saxman's Starfish program (seen here).
 
A large part of the code involves generating different linear and planar waves and merging them in different ways, such as multiplying them. Currently, I have 3 different linear wave classes implemented: CosWave, SawtoothWave, and EssWave. Each of these has a float Value(float d) function which returns a value according to the wave's frequency and amplitude (think of it as f(x)...)
 
For merging waves, I will have to allow any type of linear wave to be passed and Proce55ing will have to trust that it has a Value function. So, I made all of my linear wave classes extends LinearWave and made an empty LinearWave class which does nothing.
 
Is this the correct way to do things? How do I access the function of an instance of one of these classes?
 
Hope this makes sense!
Ryan Govostes
 
TomC

WWW
Re: classes and subclasses
« Reply #1 on: Dec 21st, 2003, 1:53am »

Yes, it sounds like you're almost there...
 
Your LinearWave class (the base class) is a generalised version of your other wave classes (the subclasses).  It needs to specify the Value function, so that all of the subclasses can be treated as LinearWave instances.
 
Here's an example...
 
Code:

 
// an array of MyBaseClass
MyBaseClass[] instances;
 
void setup() {
  instances = new MyBaseClass[3];
  // all three items in the array are different
  instances[0] = new Example0();
  instances[1] = new Example1();
  instances[2] = new Example2();
  for (int i = 0; i < instances.length; i++) {
    // all we know here is that we have three  
    // instances of MyBaseClass, but the right test  
    // function gets called automatically
    instances[i].test();
  }
}
 
// abstract here means that the class has  
// methods which must be implemented  
// by its subclasses
abstract class MyBaseClass {
  abstract void test(); // subclasses must have a test() method
}
 
class Example0 extends MyBaseClass {
  void test() {
    println("Example0");
  }
}
 
class Example1 extends MyBaseClass {
  void test() {
    println("Example1");
  }
}
 
class Example2 extends MyBaseClass {
  void test() {
    println("Example2");
  }
}
 

 
Hope that helps,
 
Tom.
« Last Edit: Dec 21st, 2003, 1:55am by TomC »  
rgovostes

rgovostes
Re: classes and subclasses
« Reply #2 on: Dec 21st, 2003, 2:41am »

Excellent, thanks! I have a long way to go before it can create spiffy images, but one small step...
 
Pages: 1 

« Previous topic | Next topic »