discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programming it's a play


From: Pascal J . Bourguignon
Subject: Re: Programming it's a play
Date: Sun, 21 Sep 2003 15:53:02 +0200

Dennis Leeuw writes:
> As I am still learning programming the following part is not something I
> knew. I had wondered what the - sign was for, but had not figured it out
> yet.
>
> > Two types of methods exists as you know : class methods (+) and instances
> > methods (-). Class methods only works on the class itself, instances
> > methods
> > only works on an instance of a class (ie, an object). imho speaking of
> > "class objects"
> > is a bit misleading -- as the definition of an object is to be an
> > instance of a class...
> > you could send messages directly to the class, but it's not an object
> > per se...
>
>
> I do want to know what this means. Can someone that writes a class, like
> the Greeter example write his own Class methods or are Class methods
> just inherited from the root class. Meaning you can only add instance
> methods.
>
> What is your view on a class (remember I am a VERY newbie). How can you
> send a message to a class when a class is not an object.
>
>  From the Apple documentation:
> http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/3objc_language_overview/index.html
> I got:
> The compiler creates just one accessible object for each class, a class
> object that knows how to build new objects belonging to the class. (For
> this reason it's traditionally called a "factory object.") The class
> object is the compiled version of the class; the objects it builds are
> instances of the class. The objects that will do the main work of your
> program are instances created by the class object at runtime.
>
> And:
> To get an object to do something, you send it a message telling it to
> apply a method.
> ...
> The receiver is an object, and the message tells it what to do.
>
> This to me means that messages can only be send to objects. That a class
> is the written definition of an object. And that the compiled version is
> a class object.

A class IS an object.

The + and - are only syntactic  tricks to allow you to define both the
structure of the class and of its instances.

While, for  optimizations reasons, the instance methods  are stored in
the class  structure, because  in Objective-C all  the instances  of a
given class  share the same method  implementations, conceptually, the
instance methods are attached to the instances.

To obtains these two objects:

 +-----------------------------+isa          +-----------------------------+
 |  MyClass : Class            |-------------|  myObject : MyClass         |
 +-----------------------------+1           *+-----------------------------+
 |  static int numOfInstance;  |             |  int x; int y;              |
 +-----------------------------+             +-----------------------------+
 |  (void)initialize;          |             |  (id)init;                  |
 |  (id)alloc;                 |             |  (void)setX:(int)X y:(int)Y |
 |  (int)howManyInstances;     |             |  (int)product;              |
 +-----------------------------+             +-----------------------------+
       *|subclass
        |          superclass +----------------+
        +---------------------| Multiplier     |
                             1+----------------+


@class MyClass
MyClass* myObject;


and be able to write:

    [MyClass howManyInstances];  // or
    [myObject product];

you just declare both in one file:

@interface MyClass:Multiplier
{
    int x;
    int y;
}
    +(void)initialize;
    +(id)alloc;
    +(int)howManyInstances;
    -(id)init;
    -(void)setX:(int)X y:(int)Y;
    -(int)product;
@end

@implementation MyClass
    static int numOfInstance;
    +(void)initialize
    {
        numOfInstance=0;
    }
    +(id)alloc
    {
        numOfInstance++;
        return([super alloc]);
    }
    // etc...
@end


Note that since there is  only one metaclass in Objective-C, you don't
have to specify the type of a class. Just @interface MyClass or @class
MyClass,  and  implicitely,  you  have  a type  declaration  of  Class
MyClass.   Also, the superclass  relation is  only a  relation between
classes,  not  instances. But  instances  inherit  the attributes  and
instance  methods defined  in the  superclass of  their class  all the
same. Note also that in Objective-C, there is no multiple inheritance:
only ONE superclass per class.

> The last two sentences are important imho. Since a class is a written
> definition it can NOT be send a message. You can only send messages to
> class objects (the compiled version).
>
> Help, help, help it gets more and more confusing.
>
> Dennis
>
>
>
> _______________________________________________
> Discuss-gnustep mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/discuss-gnustep

--
__Pascal_Bourguignon__
http://www.informatimago.com/
Do not adjust your mind, there is a fault in reality.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]