oc63--协议@protocol1

oc63--协议@protocol1
//
//  SportProtocol.h
//  day17
#import <Foundation/Foundation.h>

@protocol SportProtocol <NSObject>
// 方法声明列表
- (void)playFootball;
- (void)playBasketball;
- (void)playBaseball;
@end
oc63--协议@protocol1
oc63--协议@protocol1
//
//  Person.h
//  day17

#import <Foundation/Foundation.h>
#import "SportProtocol.h"  //相当于拷贝了一份方法的声明
@interface Person : NSObject <SportProtocol>  //继承了协议,就有了协议里面所有方法的声明。继承实类默认就有实现,继承协议没有实现,
@end
oc63--协议@protocol1
oc63--协议@protocol1
//
//  Person.m
//  day17

#import "Person.h"

@implementation Person

- (void)playFootball
{
    NSLog(@"%s", __func__);
}

- (void)playBasketball
{
    NSLog(@"%s", __func__);
}

- (void)playBaseball
{
    NSLog(@"%s", __func__);
}
@end
oc63--协议@protocol1
oc63--协议@protocol1
//
//  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;
}
oc63--协议@protocol1

上一篇:类的成员函数返回引用所引发的问题


下一篇:浙江移动首个基于阿里云技术的生产应用上线成功