协议实现多继承
协议实现多继承的话,只是简答的提供了接口,并灭有提供实现的方式。
A
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol StuAProtocal <NSObject>
// 学生A 会游泳
- (void)swimming;
@end
@interface StudentA : NSObject
@end
NS_ASSUME_NONNULL_END
#import "StudentA.h"
@implementation StudentA
@end
B
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol StuBProtocal <NSObject>
// 学生A 会游泳
- (void)running;
@end
@interface StudentB : NSObject
@end
NS_ASSUME_NONNULL_END
#import "StudentB.h"
@implementation StudentB
@end
C 遵守A 和 B 的协议
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface StudentC : NSObject
- (void)action;
@end
NS_ASSUME_NONNULL_END
#import "StudentC.h"
#import "StudentA.h"
#import "StudentB.h"
@interface StudentC()<StuBProtocal,StuAProtocal>
@end
@implementation StudentC
- (void)action{
[self swimming];
[self running];
}
- (void)running
{
}
- (void)swimming {
}
@end
本文探讨了Objective-C中如何通过协议实现多继承,展示了如何定义和遵守多个协议,以及如何在类中实现这些协议的方法。
1367

被折叠的 条评论
为什么被折叠?



