学习objective-c的一些特殊的地方

objective-c 这种语言虽然说是基于c,但是跟c的衍生语言 c++,java,c#都区别不小,看着也让我很是不爽

首先是方法调用

[circle setFillColor:kRedColor]

cricle是类名,setFillColor是方法名,kRedColor是参数值,用惯了c#的人,估计用的郁闷。

重要的两个点:

(1)objective-c 多参数方法的定义与调用

方法定义举例:

-(void)insertObject:(id)anObject atIndex:(NSInteger)index

各部分解释:

1.方法修饰符

  - 代表此方法是实体方法,必须先生成类实例,通过实例才能调用该方法。

  + 代表此方法是类的静态方法,可以直接调用,而不用生成类实例。

2.参数类型

  id 与 NSInteger 分别是两个参数 anObject 和 index的类型。

3.方法签名

  本例中,insertObject 和 atIndex组成了该方法的签名关键字。

此处举例如下:

-(void) setTo: (int) n over: (int) d

{

numerator = n;

denominator = d;

}

[aFraction setTo: 100 over: 200];//调用

注:objective-c 的方法参数名有些怪异,第一个参数是没有参数名的,如果硬要说有,那就是方法名,

统一说来,见到冒号,冒号前面那个就是参数名。

再举例,没有参数名的方法定义与调用:

-(int) set: (int) n: (int) d;

[aFraction set: 1 : 3];//调用

(二objective-c 多态 ——动态类型 id 的使用(与 C# 的比较说明)

在C#中,我们用接口来实现多态。比如接口IOb,定义了1个方法F; 有两个类A,B都实现了IOb接口。

IOb item = new A();

item.F();//执行的是A.F();

item = new B();

item.F();//执行的B.F();

在objective-c中,interface 的含义和C#有了很大的不同,不能这样使用。

那么如何实现类似的效果呢。那就是特殊类型id,看如下代码段,注释:Fraction 和 Complex都包含print 方法。

#import “Fraction.h”

#import “Complex.h”

int main (int argc, char *argv[])

{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

id dataValue;//定义了一个id 类型变量

Fraction *f1 = [[Fraction alloc] init];

Complex *c1 = [[Complex alloc] init];

[f1 setTo: 2 over: 5];

[c1 setReal: 10.0 andImaginary: 2.5];

// first dataValue gets a fraction

dataValue = f1;

[dataValue print]; //调用Fraction的 print方法

// now dataValue gets a complex number

dataValue = c1;

[dataValue print]; //调用Complex的 print方法

[c1 release];

[f1 release];

[pool drain];

return 0;

}

方法定义举例:

-(void)insertObject:(id)anObject atIndex:(NSInteger)index

各部分解释:

1.方法修饰符

  - 代表此方法是实体方法,必须先生成类实例,通过实例才能调用该方法。

  + 代表此方法是类的静态方法,可以直接调用,而不用生成类实例。

2.参数类型

  id 与 NSInteger 分别是两个参数 anObject 和 index的类型。

3.方法签名

  本例中,insertObject 和 atIndex组成了该方法的签名关键字。

此处举例如下:

-(void) setTo: (int) n over: (int) d

{

numerator = n;

denominator = d;

}

[aFraction setTo: 100 over: 200];//调用

注:objective-c 的方法参数名有些怪异,第一个参数是没有参数名的,如果硬要说有,那就是方法名,

统一说来,见到冒号,冒号前面那个就是参数名。

再举例,没有参数名的方法定义与调用:

-(int) set: (int) n: (int) d;

[aFraction set: 1 : 3];//调用

(二objective-c 多态 ——动态类型 id 的使用(与 C# 的比较说明)

在C#中,我们用接口来实现多态。比如接口IOb,定义了1个方法F; 有两个类A,B都实现了IOb接口。

IOb item = new A();

item.F();//执行的是A.F();

item = new B();

item.F();//执行的B.F();

在objective-c中,interface 的含义和C#有了很大的不同,不能这样使用。

那么如何实现类似的效果呢。那就是特殊类型id,看如下代码段,注释:Fraction 和 Complex都包含print 方法。

#import “Fraction.h”

#import “Complex.h”

int main (int argc, char *argv[])

{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

id dataValue;//定义了一个id 类型变量

Fraction *f1 = [[Fraction alloc] init];

Complex *c1 = [[Complex alloc] init];

[f1 setTo: 2 over: 5];

[c1 setReal: 10.0 andImaginary: 2.5];

// first dataValue gets a fraction

dataValue = f1;

[dataValue print]; //调用Fraction的 print方法

// now dataValue gets a complex number

dataValue = c1;

[dataValue print]; //调用Complex的 print方法

[c1 release];

[f1 release];

[pool drain];

return 0;

}