[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: objective-c question
From: |
Benedikt Stefansson |
Subject: |
Re: objective-c question |
Date: |
Mon, 12 Apr 1999 15:48:43 -0700 |
Hi,
This is a perfect example of where you should use protocols (which I belive
are similar to templates in C++ although I don't want to go out on a limb on
this). A protocol only defines the interface to methods and nothing else, i.e.
like an interface without the instance variable declaritions. So your
DemandCurve protocol file (DemandCuve.h) would be:
(#imports...)
@protocol DemandCurve
- (double)getQuantity: (double)price;
@end
And your LinearDemandCurve.h file would be:
#import DemandCurve.h
@interface LinearDemandCurve: SwarmObject <DemandCurve> {
(instance variables...)
}
@end
The LinearDemandCurve.m file would then be:
#import LinearDemandCurve.h
@implementation LinearDemandCurve
- (double)getQuantity: (double)price {
(some stuff...)
}
When you create a LinearDemandCurve object, for example in ModelSwarm, you
should cast the type as:
id <DemandCurve> demandFunction;
Now you can even switch demand functions on the fly, the compiler won't
complain because they all adhere to the same protocol.
The best Objective-C book is the one from Apple, look for the link on the
Swarm website or at Apple under their MacOS-X-Server documentation.
Regards,
Benedikt
Tamas Papp wrote:
> I'm writing a model that needs different demand curves (functions which
> assign a quantity demanded to any given price.) I thought of writing a
> "placeholder" class, DemandCurve, and subclasses like LinearDemandCurve,
> PolynomialDemandCurve, etc. I define DemandCurve as
>
> @interface DemandCurve
> {
> }
>
> - (double)getQuantity: (double)price;
> @end
>
> and then writing the subclasses. The classes which use a demand curve
> would get a pointer to a DemandCurve subclass, but use it as a DemandCurve
> because they only need the getQuantity function. How is this done in
> Objective-C? I only have experience with C++.
>
> Regards,
>
> Tamas
>
> P.S.: Does anybody know and URL for a good intoductory & reference page
> for Objective C online?
>
> ==================================
> Swarm-Support is for discussion of the technical details of the day
> to day usage of Swarm. For list administration needs (esp.
> [un]subscribing), please send a message to <address@hidden>
> with "help" in the body of the message.
--
----------------
Benedikt Stefansson address@hidden
Department of Economics, UCLA Fax. (310) 825-9528
Los Angeles, CA 90095-1477 Tel. (310) 825-1777
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.