oc——类——协议

概述

协议,protocol,是一种特殊的interface,这种特殊的interface不允许定义数据成员,允许声明Method,protocol纯粹作为接口使用,因此没有对应implementation
  • protocol不允许定义数据成员
  • protocol允许声明Method,包括instance method(-)和class method(+),protocol没有对应implementation
  • protocol可继承protocol,而且支持多继承,protocol继承本质是union父protocol的Method声明到子protocol中
  • protocol纯粹作为接口使用,因此没有实体,不能实例化
  • protocol的Method声明允许的修饰词包括@required和@optional,默认@required,@required是必须实现接口,@optional是可选实现接口
  • @required和@optional的唯一区别是对应接口没有实现时编译是否warning(@required编译warning,@optional编译不warning)

protocol

@interface FBFood : NSObject

@end

@implementation FBFood

@end

@protocol FBFeedFood1

- (void)feedRice:(int)rice andMeat:(int)meat;
- (void)feedFruit:(int)fruit andFish:(int)fish;

@end

@protocol FBFeedFood2

- (void)feedRice:(int)rice andMeat:(FBFood *)meat;
- (void)feedFruit:(int)fruit andFish:(FBFood *)fish;

@end

@protocol FBFeedFood <FBFeedFood1, FBFeedFood2>

- (void)feedFruit:(FBFood *)fruit andFish:(FBFood *)fish;

@optional
- (void)feed;

@end

@interface FBAnimal : NSObject <FBFeedFood>

@end

@implementation FBAnimal

- (void)feedRice:(int)rice andMeat:(int)meat
{
    NSLog(@"feedRice:andMeat:");
}

- (void)feedFruit:(FBFood *)fruit andFish:(FBFood *)fish
{
    NSLog(@"feedFruit:andFish:");
}

@end
总结:
  • protocol可继承protocol,而且支持多继承,protocol继承本质是union父protocol的Method声明到子protocol中,因此不可避免的会与已有Method声明相同(SEL相同),重复的Method声明的参数列表可能会不同,union时选取重复Method声明的优先级为:FBFeedFood->FBFeedFood1->FBFeedFood2(即@protocol FBFeedFood <FBFeedFood1, FBFeedFood2>的protocol定义顺序)

应用

@protocol FBFeedFood1

- (void)feedRice:(int)rice andMeat:(int)meat;

@end

@protocol FBFeedFood2

- (void)feedFruit:(int)fruit andFish:(int)fish;

@end

@protocol FBFeedFood <FBFeedFood1, FBFeedFood2>

@end

@protocol FBGuard

@required
- (void)watch;

@optional
- (void)attack;

@end

@interface FBAnimal : NSObject <FBFeedFood>
{
@public
    int _food;
}

@end

@implementation FBAnimal

- (void)feedRice:(int)rice andMeat:(int)meat
{
    NSLog(@"feedRice:andMeat:");
}

- (void)feedFruit:(int)fruit andFish:(int)fish
{
    NSLog(@"feedFruit:andFish:");
}

@end

@interface FBDog : FBAnimal <FBGuard>
{
@public
    int _vol;
}

- (void)bark;

@end

@implementation FBDog

- (void)bark
{
    NSLog(@"bark");
}

- (void)watch
{
    NSLog(@"watch");
}

@end
- (void)use_protocol
{
    NSObject<FBFeedFood> *animal1 = [[FBAnimal alloc] init];
    //animal1->_food = 5;
    [animal1 feedRice:5 andMeat:8];
    [animal1 methodForSelector:@selector(feedFruit:andFish:)];
    //[animal1 unknownMethod];
    
    id<FBFeedFood> animal2 = [[FBAnimal alloc] init];
    //animal2->_food = 5;
    [animal2 feedFruit:5 andFish:8];
    //[animal2 methodForSelector:@selector(feedFruit:andFish:)];
    //[animal2 unknownMethod];
    
    id<FBGuard> animal3 = [[FBDog alloc] init];
    //animal3->_vol = 8;
    //[animal3 feedRice:5 andMeat:8];
    //[animal3 methodForSelector:@selector(feedFruit:andFish:)];
    [animal3 watch];
    
    FBAnimal<FBFeedFood> *animal4 = [[FBAnimal alloc] init];
    animal4->_food = 5;
    [animal4 feedRice:5 andMeat:8];
    [animal4 feedFruit:5 andFish:8];
    
    FBDog<FBGuard> *animal5 = [[FBDog alloc] init];
    //animal4->_vol = 8;
    [animal5 bark];
    [animal5 watch];
    
    NSLog(@"FBAnimal confirm FBFeedFood1 = %d", [FBAnimal conformsToProtocol:@protocol(FBFeedFood1)]);
    NSLog(@"FBAnimal confirm FBFeedFood = %d", [FBAnimal conformsToProtocol:@protocol(FBFeedFood)]);
    NSLog(@"FBAnimal confirm FBGuard = %d", [FBAnimal conformsToProtocol:@protocol(FBGuard)]);
    
    NSLog(@"FBDog confirm FBFeedFood1 = %d", [FBDog conformsToProtocol:@protocol(FBFeedFood1)]);
    NSLog(@"FBDog confirm FBFeedFood = %d", [FBDog conformsToProtocol:@protocol(FBFeedFood)]);
    NSLog(@"FBDog confirm FBGuard = %d", [FBDog conformsToProtocol:@protocol(FBGuard)]);
}
output:
feedRice:andMeat:
feedFruit:andFish:
watch
feedRice:andMeat:
feedFruit:andFish:
bark
watch
FBAnimal confirm FBFeedFood1 = 1
FBAnimal confirm FBFeedFood = 1
FBAnimal confirm FBGuard = 0
FBDog confirm FBFeedFood1 = 1
FBDog confirm FBFeedFood = 1
FBDog confirm FBGuard = 1
使用protocol两种方式:
  • 类<protocol1, protocol2...> *对象名
  • id<protocol1, protocol2...> 对象名
总结:
  • 类<protocol1, protocol2...> *允许访问类+协议的成员,id<protocol1, protocol2...>只允许访问协议的成员,否则编译warning
  • 习惯使用id<protocol1, protocol2...>定义实例对象,简洁明了,因为使用protocol一般只关注protocol的Method接口(不大使用类提供的功能)
  • 类遵守协议并不确保类实现了协议的所有Method(@required不实现,编译warning,@optional不实现,编译不warning)
  • 父类遵守的protocol,子类继承遵守
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OC(Objective-C)是一种面向对象的编程语言,它支持在中使用方法和block。方法是在中定义的方法,可以直接通过名来调用,而不需要实例化对象。而block是一种闭包,可以在代码中定义并传递给方法或函数,用于延迟执行特定的逻辑。 在OC中,可以使用方法来创建和操作的实例,例如通过一个工厂方法创建对象,或者在方法中实现一些与相关的逻辑。方法通常使用“+”符号进行声明和实现。 而block可以在方法中作为参数传递,也可以在方法中定义和使用。block可以捕获其所在作用域的变量,可以在方法内部延迟执行一段代码,也可以用于实现回调等功能。block的定义和使用使用“^(){}”语法。 方法和block可以结合使用,例如可以在方法中接受一个block作为参数,并在合适的时机调用该block,以实现一些灵活的逻辑。通过方法和block的组合,可以在OC中实现更加灵活和强大的功能,例如在异步操作中使用block来回调结果,或者在工厂方法中使用block来定制对象的初始化逻辑等。 总而言之,方法和block是OC中的两个重要特性,它们可以分别用于的操作和延迟执行逻辑,也可以结合使用以实现更加灵活的功能。在实际的OC开发中,方法和block通常会被广泛使用,可以帮助开发者更加简洁和灵活地实现代码逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值