[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Would you add Hands-on coding examples at the marked points, and perhaps any additional pertinent examples? Francis
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A class interface declares instance variables, methods and
the superclass name, while the implementation file holds the
operational code that implements those methods. The interface
is included in the source using #include
:
#include "baseclass.h" |
Typically the Interface and Implementation are held in separate files, using the .h and .m extensions, respectively. They may, however, be merged into one file, and a single file may implement many classes.
To ensure that a Header file is included only once, it is usual to protect it with pre-compiler defines:
#ifndef _MY_CLASS_H_INCLUDED #define _MY_CLASS_H_INCLUDED /* HEADER FILE */ #endif |
This is the standard C technique to protect header files from being included more than once.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The interface file declares new classes that can be used by source code, holding all the information necessary to use the classes from other Objective-C code. Firstly, the file reveals to the programmer the position of the class in the class hierarchy by defining exactly which is the superclass. Secondly, it informs programmers of what variables are inherited when they create subclasses. Finally, the interface file may inform other software entities of the messages that can be sent to the class object and to the instances of the class.
The syntax of a class interface is of the form:
I would like a better example interface than this? So would you create something more real?
// Need something here. |
@interface
and @end
.
@interface ThisClass : ThisClassSuperClass
names the class
and links it to the superclass. If no superclass is named, and the
directive is without a colon, the compiler assumes that a root class
is being created.
- (float) scale; |
- (void) setRadius: (float)aRadius; |
Note. The default type for methods and messages (id
) is assumed
when a return or argument type is not explicitly declared.
For example, -name;
implicitly means a method returning id
(i.e. an object). It is usually better to avoid this and use
explicit typing as in
- (NSString*) name; |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
#include
. Thereafter the source
module may utilize the classes in those interfaces so as to:
With the exception of the root class, all working interfaces
integrate a superclass using either #include
- as was
seen in the previous simplified interface file example.
As a result the vast majority of class files begin with a
standard form that includes their superclasses, and thus
places them in the class hierarchy:
Here I have another example - would you replace it with something more meaningful?
#include "Superclass.h" @interface InterfaceName : InterfaceSuperclass { // instance variables } Declared methods |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
If you are implementing a new class, you always need to include
the interface of the superclass using #include
;
@class
cannot be used in this case because the compiler
needs to know the details of the superclass and its instance
variables etc., so as to create a fully working new class.
If you try using @class in this case, compilation will abort.
Note. When you need to send methods to an object, it's better to include the full class interface. Even in that case - it's better *only* because it allows better type checking - which is good of course - but not essential. And - to say it all - if you use @class that way, the compiler complains as soon as you try to send a message to an object of a class declared using @class, so that you know you need to include the interface for full type checking. This means it's a good idea to always try with @class if possible and only include the interface if the compiler complains that it can't type-check without the full interface.
To inform the compiler that Border and Square are classes without including their full interface file, the following syntax is used:
@class Border, Square;
Class names may also appear in interface files at times when instance variables, return values and arguments are statically typed:
- (void) set
Another example of a class interface is required here, and perhaps an example of a class cluster would be useful as this is mentioned in chapter 1.
a simple example of a class interface <fixme: what example ?>
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In the C programming language C structures are singular entities that encapsulate multiple data elements. The fields therein reside in name spaces so they do not interfere with alike named entities that are outside the structure. So the name spaces define another partition.
Such C structures relate to C functions that provide the application logic or defined mechanisms. Within a C structure data elements may be local to a function, and are protected in their name space.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
#include "ClassTitle .h" @implementation ClassName :Superclass { // instance variables } // methods @end |
The Implementation file uses #include
to include a named
interface file holding all declarations. Thereafter, the
implementation need not declare instance variables, rather it
may be dedicated to defining methods that may include a number
of arguments; these are declared in the same way as the interface
file but without the semicolon:
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When an instance method of a certain object is called, the method can refer to the object's instance variables directly by name.
The following example illustrates how a method definition refers to the
receiving object's instance variable called line
.
- (void) setLine: (BOOL)flag { line = flag; } |
You can access instance variables of other instances of the same class by using the operator ->
.
Example here.
The following code fragment demonstrates how the GPRS class may declare a statically typed object as an instance variable called qosfive:
@interface GPRS :NSObject { Connect *qosfive; int frequency; struct features *chargeband; } |
Because qosfive is typed to the same class, the instance variables of the statically typed class are in the scope of the class. So the Connect method may set them directly:
-makeAnotherQosfive { if (!qosfive) { qosfive =[[Connect alloc ] init ]; qosfive->frequency =frequency; qosfive->chargeband =chargeband; } return qosfive; } |
Note. When the object is not a receiver, it is necessary that the object be statically typed in the class declaration.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In Objective-C instance variable have three types of scope or accessibility:
@private
restricts the instance variable to the declaring class, and not to inheriting classes.
@protected
restricts the instance variable to the declaring class and inheriting
classes - this is the default scope.
@public
globalises the instance variable, removing all the aforementioned restrictions.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The @defs(className)
directive produces a declaration list, where public
is used as a pointer to the structure that approximates an instance of Gprschannel
:
struct gprschannelDef { @defs(Gprschannel) }*public; |
An object's instance variables may be given public scope through a Gprschannel id
assigned to the pointer:
id aGprschannel; aWorker =[[Gprschannel alloc ] init ]; public =(struct gprschannelDef *)aGprschannel; public->boss =nil; |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In the implementation of an Objective-C method, you may use the two reserved words self and super
example here . In Objective-C some control over messaging is provided by self and super. Self searches for the method implementation in the receiving object's class.
Super begins searching for the method implementation in the superclass of the class that defines the method with super.
A method implementation may refer to the target object as self or super, where control is provided over which object performs the method.
To demonstrate the difference between self and super consider three classes:
All three classes define a method called traffic, while Two defines a method called Alert that depends on traffic.
Sending a message to the One object to invoke the Alert method, causes the Alert method to send a traffic message to the same One object.
The following source code would search for the traffic method defined in One (or self's class).
-Alert { [self traffic ]; ... } |
When Two’s source code calls this object super, the messaging routine will find the version of traffic defined in Three (the superclass of Two).
-Alert { [super traffic ]; ... } |
Self is a variable name that may be assigned new values particularly in definitions of class methods that often focus on class instances as opposed to class objects. For example, a method might combine allocation and initialization of an instance:
+(id)newRect { return [[self alloc ] init ]; } |
When in class methods self refers to class objects, and when in instance methods self refers to instances. self and super focus on the receiver that is the object being stimulated by a message.
Nicola/Richard add an example for self and super here - or two examples?
implementation of the example class interface
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A category interface has the form:
#include "ClassTitle .h" @interface ClassTitle (CategoryTitle ) declared methods @end |
The category may include a interface, so as to allow its methods to access the instance variables of named classes.
A category implementation file has the form:
#include "CategoryTitle .h" @implementation ClassTitle (CategoryTitle ) definitions of methods @end |
Category methods may replace the conventional methods inherited by the class.
Note. A category should not be considered a substitute for a subclass.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |