Re: OOP Clarification: difference between classes and instances, compare with C++
Re: OOP Clarification: difference between classes and instances, compare with C++
- Subject: Re: OOP Clarification: difference between classes and instances, compare with C++
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 1 Jan 2002 04:50:24 -0800
On Tuesday, January 1, 2002, at 04:25 AM, Thomas Lachand-Robert wrote:
>
Le lundi 31 dicembre 2001, ` 11:48 , Stephen A. Cochran a icrit :
>
>
> I've read about the difference between a class method and a
>
> instance method
>
> before, but the difference has somehow become lost in my
>
> cobwebs. All I can
>
> remember is the difference is subtle, but an important concept.
>
>
>
It is important, but there is nothing subtle or mysterious in
>
the difference. Since it is a frequently asked question, I will
>
try a detailed answer, and do a comparison with C++ that many
>
people already know there.
>
>
>
Suppose you define a class like that:
>
@interface MyClass : NSObject {
>
int member1, member2;
>
float otherMember;
>
}
>
>
+(void)doSomething;
>
>
-(id)init;
>
>
-(void)aMethod;
>
>
@end
>
>
and then use it somewhere like that:
>
MyClass *a = [[MyClass alloc] init];
>
[a aMethod];
>
>
Then your program creates an object 'a' which is basically a
>
struct containing
>
{
>
/* here some additional intendance members */
>
int member1, member2;
>
float otherMember;
>
}
>
(It turns out that the intendance members is a pointer named
>
_isa; but you don't have to care usually.)
>
Such an object is called an 'instance' of the class MyClass,
>
and you can create as many as you need.
>
>
As soon as you begin to use the class, here for the first
>
[MyClass alloc], the program automatically creates an
>
additional object named MyClass: that is the class object.
>
There is only one of them for each class defined, and it
>
doesn't contain any member (except isa, again). You can send it
>
any message (like always in Obj-C), but it responds only to the
>
messages defined with a '+' in front (like 'doSomething' in my
>
example), not to those with a '-' (these ones are for the
>
instances of the class, like 'init', 'aMethod'). Hence you can
>
do
>
[MyClass doSomething];
>
[a aMethod];
>
but not
>
[MyClass aMethod]; // NO! MyClass is not an instance of 'MyClass'
>
[a doSomething]; // NO! doSomething is a class method
>
>
Note that inside each method, you can use 'self': it refers to
>
the object for which the method was called, hence the class in
>
'doSomething', the instance in 'aMethod'. For every instance
>
object, [self class] gives the corresponding class object.
>
>
Important: any class object gets automatically a few methods.
To be precise: any class that inherits from NSObject will get
these methods. Most "normal" classes you write will inherit from
NSObject, but there are cases (such as NSProxy) where this is
not necessarily true. If you're doing something tricky, like
implementing your own remote messaging or undo facility, you may
have occasion to write your own root class, in which case you
will *not* inherit an +alloc, or any other methods.
>
The most important is 'alloc': it tells the class object to
>
create a new instance. Hence a = [[MyClass alloc] init] does
>
the following:
>
1/ creates the clas object if it doesn't exist already;
>
2/ send the message 'alloc' to the class object: this must
>
return an object (an instance of the class, usually, but not
>
always: it could be an instance of a hidden subclass);
>
3/ send the 'init' message to the new created object, and
>
copies the returned value in 'a'.
>
>
It is important to always chain these calls like that, since it
>
allows the 'init' method to modify the object and return a
>
different one, if needed. So DON'T write
>
a = [MyClass alloc]; [a init]; // NO!
>
(My feeling is that is the nested [[MyClass alloc] init] who
>
confuses people about classes and instances... but it is
>
important to do this chaining.)
>
>
Another important class method is 'initialize' (more precisely
>
+(void)initialize). It is called when the class object is
>
created, hence it is the 'init' method of the class object. It
>
defaults to doing nothing,
>
but you can override it to create global variables for example.
Actually, +initialize is called before any other method in the
class is called. That's not necessarily the same as "when the
class is created."
Also, categories can have an +initialize method as well, which
will be called before any other method in that category.
>
One more magic method is +load, which will be called as soon as
the class or category it's in is loaded in the running program,
even before +initialize. +load is an opportunity to do such
things as install posing classes (see +poseAsClass: for
description.)
-jcr
Support your right to protest a criminal nut-cult.
http://freehenson.da.ru/