home *** CD-ROM | disk | FTP | other *** search
- Objective:
-
- Learn how to initialize object state variables by using the "new" method.
-
- Terms:
-
- Super Class: Your parent class. When you send a message to your
- "super" you are sending it to your "Super Class"
-
- Discussion:
- In traditional programming, programmers think of a program
- in which the program counter starts at the top of a pogram,
- (typically called the "main" section) and continues in a
- linear manner through the end of the program. In event based
- programming we think of a two dimensional array of "events" which
- send "action messages" to various sections of code which respond to
- these "events". The order of when each section of code is executed
- is not dependant on the order it appears in the file but on the order
- that these events occur.
-
- Method:
- Copy the events module into a seperate tree and then add the following to
- the myObject.h file:
-
- /* Generated by Interface Builder */
-
- #import <objc/Object.h>
-
- @interface MyObject:Object
- {
- int myInt;
- }
-
- + new; /* this is the only line that we are adding */
- - event1:sender;
- - event2:sender;
-
- @end
-
- Copy the myObject.m file and add the following method after the
- @implementation line:
-
- + new // this section of code will get run only once
- {
- self = [super new]; // create new instance of myObject by sending the
- // "new" message to our superclass plus run the
- // following code:
- myInt = 100;
- printf("new myInt = %d\n", myInt);
- return self;
- }
-
- Type "make" you will see that the "new" method gets executed only once
- when the program starts up.
-
- Futher Suggestions:
-
- Summary:
- You now know how to initialize internal state variables of an object.
- All initialization code for objects should go in the new method.
-
-