Objects,Messages,Classes

Objects:

As the name implies, object-oriented programs are built around objects.
An object associates data with the particular operations that can use or affect that data.
In Objective-C, these operations are known as the object’s methods; the data they affect are its instance variables.

In Objective-C, objects are identified by a distinct data type, id.This type is
defined as a pointer to an object
////id anObject;///
nil -->objc.h@/NextDeveloper/Headers.

Messages:

To get an object to do something, you send it a message telling it to apply a
method. In Objective-C, message expressions are enclosed in square brackets:
[receiver message]

(30.0, 50.0):==> [myRect setOrigin:30.0 :50.0];
setWidth:height: ==> [myRect setWidth:10.0 height:15.0];
[receiver makeGroup:group, memberOne, memberTwo, memberThree];

Classes:

In Objective-C, you define objects by defining their class. The class definition
is a prototype for a kind of object; it declares the instance variables that become part of every member of the class, and it defines a set of methods that all objects in the class can use.

NSObject --> NSObject, being a root class, doesn’t have a superclass. In OpenStep, it’s in the inheritance path for every other class.
int i = sizeof(Rectangle);
Rectangle *myRect;
if ( [anObject isMemberOfClass:someClass] )
if ( [anObject isKindOfClass:someClass] )
id aClass = [anObject class];
Class aClass = [anObject class];
Creating Instances=> [Rectangle alloc]
The alloc method dynamically allocates memory for the new object’s instance
variables and initializes them all to 0—all, that is, except the isa variable that
connects the new instance to its class.

Defining A Class:
In Objective-C, classes are defined in two parts:
interface=>
@interface ClassName : ItsSuperclass
{
instance variable declarations
}
method declarations
@end
class methods ->+ alloc;
instance methods -> - (void)display;
- (float)radius;
- (void)setWidth:(float)width height:(float)height;
- makeGroup:group, ...;
Importing the Interface -> #import "Rectangle.h"
Referring to Other Classes -> @class Rectangle, Circle;
An interface file mentions class names when it statically types instance variables,return values, and arguments.
The Implementation=>
@implementation ClassName : ItsSuperclass
{
instance variable declarations
}
method definitions
@end
@private The instance variable is accessible only within the class that declares
it.
@protected The instance variable is accessible within the class that declares it
and within classes that inherit it.
@public The instance variable is accessible everywhere.

How Messaging Works:
[receiver message]->objc_msgSend(receiver, selector)

你可能感兴趣的:(C++,c,C#,prototype,Objective-C)