Re: OOP Clarification
Re: OOP Clarification
- Subject: Re: OOP Clarification
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 1 Jan 2002 04:15:18 -0800
On Monday, December 31, 2001, at 09:34 PM, Adam Eijdenberg wrote:
>
> 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.
>
>
A class method applies to the the entire class, rather than an
>
individual instance. That is, a class method has no reference
>
to 'self' (or 'this' depending on the language used). Basically
>
it's just a method that logically should be with the class
>
definition, but isn't a true "method" as such that it applies
>
to an object, it's more like a "function" in procedural
>
programming.
This isn't correct for Objective-C. In Obj-C, the class itself
is an object, which creates instances and manages data pertinent
to the class. Class methods are also referred to as "factory
methods" in Obj-C, since the class can be thought of as a
factory for instances of the class.
[NSObject alloc] is a class method of NSObject, which tells the
class to allocate memory for an instance. [NSColorPanel
sharedColorPanel] is a class method of NSColorPanel, which
returns the color panel (of which there should only be one.)
In a class method, "self" (as always) refers to the receiver of
the method, which in the case is the class, not an instance of
the class.
-jcr
"The problem with trying to child-proof the world, is that it
makes people neglect the far more important task of
world-proofing the child." -- Hugh Daniel