Objective-C introduction - 4

去我的目录

我们之前说到Objective-C 是一种很好的面向对象的语言,和C++Java 相比,Objective-C 有一些自己独特的东西,下面我们来简单的介绍一下。

 

1)Category

回想一下,在C++ 中,如果我们想继承一个类,给它添加一些新的功能,我们需要什么?当然是我们需要得到这个类的源代码。但是在Objective-C 中,由于有了这个Category 的概念,我们可以在没有源代码的情况下,为一个已经存在的类添加一些新的功能,比如:

 

// DisplayMath.h

@interface Rectangle(Math)

- (int) calculateArea;

- (int) calculatePerimeter;

@end


// DisplayMath.m

@implementation Rectangle(Math)

- (int) calculateArea {

    return width*height;

}

- (int) calculatePerimeter {

    return 2*(width+height)

}

@end

 

这里,使用了之前定义的Rectangle 类,我们想为它添加两个功能:计算面积和周长。即使没有Rectangle 类的源代码,我们也可以通过Category 创建Rectangle 的子类。

使用Category的时候,需要注意两个地方:

1) 只能添加新的方法,不能添加新的数据成员

2)Category 的名字必须是唯一的,比如这里,就不允许有第二个Math 存在。

 

如何使用:

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

    Rectangle *rect = [[Rectangle alloc] initWithWidth: 5 andHeight:10];

    [rect calculateArea];

    [rect release];

}

 

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

2)如何创建私有方法

我们知道可以使用@private来声明私有变量,但是如何声明私有方法呢?

Objective-C 中并没有什么关键词来修饰方法,如果我们想让某个方法不被其他的类所见,唯一的方法就是不让这个方法出现在头文件中。比如:

// MyClass.h

#import <Foundation/NSObject.h>

@implementation MyClass

- (void) sayHello;

@end


// MyClass.m

#import "MyClass.h"

@implementation MyClass

- (void) sayHello {

    NSLog(@"Hello");

}

@end


@interface MyClass(Private)

- (void) kissGoodbye;

@end

@implementation MyClass(Private)

- (void) kissGoodbye {

    NSLog(@"kissgoodbye");

}

@end

 

怎么样,看到了Category 的应用么?是的,利用Category 可以方便的实现“私有”方法。

 

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

3)Protocol

Objective-C 中,Protocol 的概念很象Java 中的interface 或者C++ 中的virtual class

来看下面这个例子:

@protocol Printing

- (void) print;

@end


// MyDate.h

@interface MyDate: NSObject <Printing> {

    int year, month, day;

}

- (void) setDateWithYear: (int)y andMonth: (int)m andDay: (int)d;

@end


// MyDate.m

#import <stdio.h>

#import "MyDate.h"

@implementation MyDate

- (void) setDateWithYear: (int)y andMonth: (int)m andDay: (int)d {

    year = y;

    month = m;

    day = d;

}

- (void) print {

   printf( "%4d-%2d-%2d", year, month, day );

}

@end

 

我们首先声明了Printing 协议,任何遵守这个协议的类,都必须实现print 方法。在Objective C 中,我们通过<>来表示遵守某个协议。当某个类声明要遵守某个协议之后,它就必须在.m文件中实现这个协议中的所有方法。

如何使用:

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

    MyDate * dat = [[MyDate alloc] init];

    [dat initDateWithYear:1998 andMonth:09 andDay:01];

   

    id<Printing> var = dat;

    [var print];


    if( [dat conformsToProtocol:@protocol(Printing)] == YES ) {} // true


    [dat release];

}

 

注意两个地方:1)使用id<Printing> 作为类型,而不是象C++中,使用Printing* var; 2)conformsToProtocol 类似于之前所说的respondsToSelector ,用于动态检查某个对象是否遵守某个协议。

 

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

 

<script type="text/javascript"> </script> <script type="text/javascript"> </script>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值