// // SportProtocol.h // day17 #import <Foundation/Foundation.h> @protocol SportProtocol <NSObject> // 方法声明列表 - (void)playFootball; - (void)playBasketball; - (void)playBaseball; @end
// // Person.h // day17 #import <Foundation/Foundation.h> #import "SportProtocol.h" //相当于拷贝了一份方法的声明 @interface Person : NSObject <SportProtocol> //继承了协议,就有了协议里面所有方法的声明。继承实类默认就有实现,继承协议没有实现, @end
// // Person.m // day17 #import "Person.h" @implementation Person - (void)playFootball { NSLog(@"%s", __func__); } - (void)playBasketball { NSLog(@"%s", __func__); } - (void)playBaseball { NSLog(@"%s", __func__); } @end
// // main.m // day17 #import <Foundation/Foundation.h> #import "Person.h" #import "Student.h" #import "Teacher.h" int main(int argc, const char * argv[]) { Person *p = [Person new]; [p playFootball]; [p playBasketball]; [p playBaseball]; Student *stu = [Student new]; [stu playBaseball]; Teacher *tea = [Teacher new]; [tea playBasketball]; return 0; }