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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值