Re: OOP Clarification
Re: OOP Clarification
- Subject: Re: OOP Clarification
- From: Rosyna <email@hidden>
- Date: Tue, 1 Jan 2002 05:33:21 -0700
The way I've always thought of it....
Class methods work only on the class, they will not work on any
instance of the class. Class methods are usually (except for
+(id)alloc) autoreleased and usually return an instance of the class.
Instance methods only work on existing variables of the class
(instances) and may or may not be autoreleased (see NSCopying,
-(id)copy for one that isn't autoreleased) and may or may not return
an instance of the class (like NSBundle's
-(NSString*)bundleIdentifier).
NSString* bob=[NSString stringWithString:@"Hi"]; // Class method, no
variable is passed to it, but an autoreleased "copy" of NSString is
returned.
NSString* bob=[[NSString alloc] initWithString:@"Hi"]; // alloc is a
class method, initWithString is an instance method, it has to work on
an existing instance (in this case returned by [NSString alloc]) and
now bob needs to be released, but only because you used alloc...
[bob stringWithFormat:@"%d",err]; //illegal, bob is an instance, not a class.
Make any sense?
Ack, at 12/31/01, Stephen A. Cochran said:
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.
I can't find a good resource. Anyone know a good
reference/definition/example of
the difference?
--
Sincerely,
Rosyna Keller
Technical Support/Holy Knight/Always needs a hug
Unsanity: Unsane Tools for Insane People