home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Tutorial / Cookbook / 04.new / notes < prev    next >
Encoding:
Text File  |  1993-01-18  |  1.8 KB  |  62 lines

  1. Objective:
  2.  
  3. Learn how to initialize object state variables by using the "new" method.
  4.       
  5. Terms:
  6.  
  7.    Super Class:  Your parent class.  When you send a message to your
  8.    "super" you are sending it to your "Super Class"
  9.  
  10. Discussion:
  11.     In traditional programming, programmers think of a program
  12.     in which the program counter starts at the top of a pogram,
  13.     (typically called the "main" section) and continues in a
  14.     linear manner through the end of the program.  In event based
  15.     programming we think of a two dimensional array of "events" which
  16.     send "action messages" to various sections of code which respond to
  17.     these "events".  The order of when each section of code is executed
  18.     is not dependant on the order it appears in the file but on the order
  19.     that these events occur.
  20.     
  21. Method:
  22. Copy the events module into a seperate tree and then add the following to
  23. the myObject.h file:
  24.  
  25. /* Generated by Interface Builder */
  26.  
  27. #import <objc/Object.h>
  28.  
  29. @interface MyObject:Object
  30. {
  31.     int myInt;
  32. }
  33.  
  34. + new;           /* this is the only line that we are adding */
  35. - event1:sender;
  36. - event2:sender;
  37.  
  38. @end
  39.  
  40. Copy the myObject.m file and add the following method after the
  41. @implementation line:
  42.  
  43. + new                    // this section of code will get run only once
  44. {
  45.    self = [super new];  // create new instance of myObject by sending the
  46.                         // "new" message to our superclass plus run the
  47.             // following code:
  48.    myInt = 100;
  49.    printf("new myInt = %d\n", myInt);
  50.    return self;
  51. }
  52.  
  53. Type "make" you will see that the "new" method gets executed only once
  54. when the program starts up.
  55.  
  56. Futher Suggestions:
  57.  
  58. Summary:
  59. You now know how to initialize internal state variables of an object.
  60. All initialization code for objects should go in the new method.
  61.    
  62.