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_
   Suggestions
   Software Suggestions
(Moderator: fry)
   questions about objects, classes, OOP
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: questions about objects, classes, OOP  (Read 671 times)
adrien

WWW
questions about objects, classes, OOP
« on: Mar 6th, 2003, 10:40pm »

 
//  a very basic Object script
 
//  can it be any shorter?
//  adrien@boring.ch
 
//  sections and lines numbered for later refernce in comments.
 
  // 1
myBallClass foo = new myBallClass(4, 8, 16);
myBallClass bar = new myBallClass(8, 16, 32);
myBallClass bla = new myBallClass(16, 32, 64);
 
 
  // 2
void setup()
{
  size(100, 100);
}
 
  // 3
void loop()
{
  foo.drawBall();  // 3.1
  bar.drawBall();
  bla.drawBall();
}
 
  // 4
class myBallClass  // 4.0.0
{
  int theBallX;    // 4.0.1
  float theBallY;
  float theBallDiam;
 
    // 4.1
  myBallClass(int ballX, int ballY, int ballDiam) {  //4.1.0
    theBallX = ballX;  // 4.1.1
    theBallY = ballY;
    theBallDiam = ballDiam;
  }
 
    // 4.2
  void drawBall() {  //4.2.0
    ellipse(theBallX, theBallY, theBallDiam, theBallDiam);  //4.2.1
  }
   
}
 
 
/*
 So I really want to understand OOP in P5.
 
 especially section 4.1
 
 My background is in ActionScript (AS) with Flash, which is heavily
 object oriented no matter what you do, although I still tend to shy
 away from 'real' OOP in AS (like making classes and stuff), as I still
 am not confortable using OOP there, although it all makes sense, it
 still doesn't all make sense ... in that weird OOP way.
 
 And I really want my students to undertstand OOP in P5, which is
 why I'm asking for feedback here to make sure I'm telling them the
 good info and not my own shaky 'understanding'
 
 
1 Creating instances of an object. Clear enough. But why do I repeat the  
 class name twice (once before the instance name, once after the 'new')?
 Is it like declaring a data type for a variable? If so, why isn't it  
 an 'instance' data type? Or a void?
 
 
2 no questions.
 
 
3 Calling the drawBall method of the instances.
 
 digression(){
  In the example on the P5 web site, this function is called just
  'draw()', but this turns out to be a mysterious third structure section
  like setup() and loop(), and so I was cautious about using and identifier
  which might be 'reserved'. should I be?
 
  Is there a way that objects can take care of doing this themselves?
  Could an object have it's own internal 'void loop()' function that will  
  take care of drawing itself, or somehow respond to the main 'void loop()'  
  like a MovieClip in AS can have an onClipEvent(enterFrame) handler?
 
  subDigression(){
   Why doesn't P5 have handlers?;
  }
 }
 
 
4 here's where I get unclear
 
4.0.0 making a class, OK
 
4.0.1 the variables for the class. OK. My ball as an X and Y and a diameter.
 
 
4.1 WTF? what does this block of code do? why do I have an extra
 set of variables? I don't understand. To me it seems like my object
 could function without it, but that's not the case. This is the part
 which really bothers me, and I really need to understand what this
 does and why. Scream(), Shout(), Curse()!
 
4.2 Yeah, OK, a ball has methods, and drawing itself is one of them.
 
 If I want the ball to move, I might make an updatePosition() method,
 and then place the a call to the updatePosition() method and drawBall()
 method in a general doTheBallThing() method which is called in void loop().
 
 See the digression in my comments on section 3.
 
 
thanks for reading.  */

 
I'm posting this here in 'syntax' rather than in 'programs' because this program is just an example to illustrate my questions...
« Last Edit: Mar 6th, 2003, 10:46pm by adrien »  

Signature: Signatures are displayed at the bottom of each post...
adrien

WWW
handlers
« Reply #1 on: Mar 6th, 2003, 10:42pm »

my bad, P5 does have handlers, like for the keyboard and mouse. duh. sorry.
 

Signature: Signatures are displayed at the bottom of each post...
adrien

WWW
Re: questions about objects, classes, OOP
« Reply #2 on: Mar 8th, 2003, 3:40pm »

OK, i'll enlarge my question:
 
Would it be useful for beginners to have a really easy class creation system?
 
would it technically be possible in P5?
 
an example code:
 
 
 
void setup()
{
 object fooBar = new baHumbug(4, 8, 16);
}
 
 
void loop()
{
 //standard...
}
 
 
class baHumbug(int bla, float bork, int asdf){
 
 void setup(){
  //code which executes at the 'birth' of an object
 }
 
 void loop(){
  //code which executes all the time like the main loop();
  doHumbug();
 }
 
  void doHumbug() {
   rect(bla, bork, asdf, asdf);
   bla += 1;
  }
}

 
 
so, the main program has three sections, setup(), loop(), and a class.
 
the main setup() and loop() are, of course, identical to what we know already.
 
in the main setup(), i have created an instance of my byHumbug object. note that the syntax is
 
object instancename = new classname(parameters)

 
or this could also look like:
 
instancename = new classname(parameters)

or
object instancename = classname(parameters)

 
 
 
in the class section, the declaration is followed by parenthesis, which declare the variables that this class will have.
 
then, inside the class, it has a similar structure to a standard proce55ing program, with it's own setup() and loop(), as well as any other functions.
 
advantages: well, it will be 10000000% easier to explain this to my students that what I have in the previous comment!!!
 
it's quick and dirty and easy. a lot less code, less chance of errors, easier to debug, etc.
My students have enough trouble making sure all the semicolons and parentheses are correct in a 20 line program; making an object from memory will be impossible for them in the semester, as it is now.
 
like many things in proce55ing, there is a simple way to do it, and there is a 'standard' java way of doing it, where the simply way can be used by beginners and non beginners who just want to make something fast, and geeks can always use the 'hard' way.
 
just a thought.
 

Signature: Signatures are displayed at the bottom of each post...
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: questions about objects, classes, OOP
« Reply #3 on: Mar 8th, 2003, 7:09pm »

hi, re: your third post, you don't need setup() and loop() inside your user-defined object class. you can refer to http://www.proce55ing.net/reference/data.html#Object ...  
 
... i mean, you can perhaps do another thing than call a setup() and a loop() inside that class. i'm not even sure if loop() will work.
 
hmm... class Something() ... are actually inner classes. would it help you if you just made separate files such that they may visually see each class object?
« Last Edit: Mar 8th, 2003, 7:15pm by Martin »  
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: questions about objects, classes, OOP
« Reply #4 on: Mar 8th, 2003, 7:30pm »

We have some nice slides to introduce OOP. You might want to use them.
 
http://aegis.ateneo.net/cs21a/Objects.ppt
 
Mike Davis

WWW
Re: questions about objects, classes, OOP
« Reply #5 on: Mar 8th, 2003, 9:40pm »

1
(See 4.1)
 
3
There shouldn't be any problems using draw(), and I haven't had any.
 
4.1
This is called the "constructor" of your class.  It's the function that creates an instance of your object.  This is the function you're calling in that second use of the class name in Q1.  In your example code, you're using a constructor that accepts some arguments so that the instance can be initialized with some values.  You can even have multiple constructors, if they accept differant arguments (this is true for any function).  If you don't define a constructor for your class, the compiler automatically adds one during compilation that does nothing but create an instance.
 
benelek

35160983516098 WWW Email
Re: questions about objects, classes, OOP
« Reply #6 on: Mar 9th, 2003, 12:42am »

on Mar 8th, 2003, 3:40pm, adrien wrote:
class baHumbug(int bla, float bork, int asdf){
 
      void setup(){
            //code which executes at the 'birth' of an object
      }
      
      void loop(){
            //code which executes all the time like the main loop();
            doHumbug();
      }
 
       void doHumbug() {
             rect(bla, bork, asdf, asdf);
             bla += 1;
       }
}

there's no problem with creating functions called loop(), etc for each of ur objects, but those functions will have nothing to do with the global methods of the same name.
 
the problem (conceptually) with having sub-implementations of the loop() and/or setup() from within the class, lies in the area of threading...
 
ie, if u have more than one of these objects running loop() at the same time, then how will you be telling java what order to do all the loop in which object should be treated first at each turn of the loop()
 
for instance...
Code:

 
baHumbug moo1 = new baHumbug(bla, bork, asdf);
baHumbug moo2 = new baHumbug(bla, bork, asdf);
 
//now the program runs, and both objects are trying to run their individual loop(). which one goes first?
 

 
-bnlk
 
adrien

WWW
Re: questions about objects, classes, OOP
« Reply #7 on: Mar 9th, 2003, 2:14pm »

on Mar 9th, 2003, 12:42am, benelek wrote:

there's no problem with creating functions called loop(), etc for each of ur objects, but those functions will have nothing to do with the global methods of the same name.
 
the problem (conceptually) with having sub-implementations of the loop() and/or setup() from within the class, lies in the area of threading...
 
ie, if u have more than one of these objects running loop() at the same time, then how will you be telling java what order to do all the loop in which object should be treated first at each turn of the loop()
 
for instance...
Code:

 
baHumbug moo1 = new baHumbug(bla, bork, asdf);
baHumbug moo2 = new baHumbug(bla, bork, asdf);
 
//now the program runs, and both objects are trying to run their individual loop(). which one goes first?
 

 
-bnlk

 
yeah, threading would logically have to be addressed.
 
in ActionScript (not that P5 should be AS, but just to show an example) a Movie Clip, which is pretty much an object as one would think of it in OOP, can have it's own onClipEvent(load) and onCLipEvent(enterFrame) handerls. The (load) event is when the MC first 'appears' in animation, and the (entreFrame) happens at the interval of the main 'clock' of the animation.
 
It works, somehow. Ask Gary Grossman! Actually, I think I might research the quesion, now that you bring it up, i never gave it much thought (ah, the disadvantage of having everything taken care of for you!)  
 
In any case, I think threading issues can be solved somehow, and (at least for my students) it might be worth the while to have a 'simple' object system in P5.
 
I'm starting to get a better grasp of the structure of an object in P5, thanks for your help, all.
 
I'm wondering what Fry and Reas (and the other folks working on P5) think of my proposal for a simple OOP in P5. just to be precise:
 
1. simplified and thin syntax for creating objects
 
2. objects (can) have an internal setup() and loop() do do their thing witout having to be told to by the main loop()

 

Signature: Signatures are displayed at the bottom of each post...
fry


WWW
Re: questions about objects, classes, OOP
« Reply #8 on: Mar 10th, 2003, 1:10am »

it's possible to make threaded objects by having your class implement "Runnable", meaning that it has a method called run() inside, which is run as a thread. (this is just regular java syntax)
 
Code:
class LittleThing implements Runnable {
  Thread thread;
 
  public LittleThing() {
    thread = new Thread(this);
    thread.start();
  }
 
  public void run() {
    while (Thread.currentThread() == thread) {
  // call loop() here, or do something
 
  try {  
    Thread.sleep(100);  
  } catch (InterruptedException e) { }
    }
  }
}

alternatively, you could just have loop() cycle throuhg each of your child objects and call their loop() method.. this would keep all the objects sync'ed with one another, since as separate threads with their own loop(), there's no guarantee that they'll stay sync'ed without lots of extra code.
 
it's sort of an interesting idea, that if you want other threaded objects maybe that needs to be simpler (the code above is a little too java-oriented). though it gets weird if you say 'loop()' is magically called in subclasses (and who knows what order it happens in) but then draw() doesn't.. or setup() doesn't.. could get messy.
 
i wonder if a better idea would be a simple built-in helper class for threaded things.. BLoopy, errr not. this would make it simpler without a language hack.
 
(moving thread to suggestions as per adrien's request)
 
_martin
Guest
Email
Re: questions about objects, classes, OOP
« Reply #9 on: Mar 10th, 2003, 2:51am »

have a multithreading example here... http://studio.instituteofmedia.com/experiments/multithread/ ... perhaps this is one of its ramifications.
 
adrien

WWW
Re: questions about objects, classes, OOP
« Reply #10 on: Mar 10th, 2003, 12:34pm »

well, like I said, my main concern is that there is a system for using OOP type structures in the most simple, straghtforward, compact but clear, and efficient way posible.
 
if a object can have it's own internal animation code, without having to make a call to the object from the main loop() block, it will make it that much more easier for a beginner to get it to work, one less hook to tie in with other code, and thus less complication, less bugs, and easier 'understanding' of the code.
 
also, a 'simplified' structure of the object system (basically dropping the object's internal constructor function), would help a lot for a beginner.
 
fry/reas/others: is it possibe? it is 'not a pain in the ass' enough to be a possibility?
 
(sorry to be a bug about it, but don't want the suggestion to be about multithreading technology, just about simplicity in objects...
The best thing for my students is when things are as simple as possible, and they can start working with a 'tool' as quickly as possible to integrate it into their bag of tricks, with a minimum of overhead of bla bla bla on my part. Of course, later, they will want to move on to more advanced methods, but it works better once they have already done it a simple way. so far, P5 has pretty much done this for them.)
 
 
coming back to mike davis's comment
Quote:

4.1
This is called the "constructor" of your class.  It's the function that creates an instance of your object.  This is the function you're calling in that second use of the class name in Q1.  In your example code, you're using a constructor that accepts some arguments so that the instance can be initialized with some values.  You can even have multiple constructors, if they accept differant arguments (this is true for any function).  If you don't define a constructor for your class, the compiler automatically adds one during compilation that does nothing but create an instance.

 
what would this look like in P5? I have tried to write the object without this, but it doesn't parse. is there alredy a simpler structure for creting an object then the one I had in the first post?
 

Signature: Signatures are displayed at the bottom of each post...
REAS

WWW
Re: questions about objects, classes, OOP
« Reply #11 on: Mar 10th, 2003, 2:58pm »

you don't need a contructor. example:
 
Code:

Module mod1;  
 
void setup() {  
  size(200, 200);  
  mod1 = new Module();  
}  
 
void loop() {  
  mod1.update();  
  mod1.display();  
}  
 
class Module {  
  float angle = 0.0;
 
  void update() {  
    angle += 0.01;
  }  
 
  void display() {  
    translate(100, 100);
    rotate(angle);
    rect(-60, -60, 120, 120);  
  }  
}  

 
Regarding each object having its own thread, my own opinion (at the moment) is that it's more clear to call all methods for classes within loop. Students know what loop is and how it works. I think it's an easy mental model to think of the loop as the controller which tells each object when to update, draw, etc.
 
adrien

WWW
Re: questions about objects, classes, OOP
« Reply #12 on: Mar 10th, 2003, 10:23pm »

on Mar 10th, 2003, 2:58pm, REAS wrote:
you don't need a contructor. example: ...

 
cool. that's what I was looking for.
 
and if I need to pass it on a bunch of variables to get a red 20 pixel circle rather than just a generic circle, I will need to have a constructor function. OK. i can explain that!
 

Signature: Signatures are displayed at the bottom of each post...
Pages: 1 

« Previous topic | Next topic »