协议学习

//
//  PlayerProtocol.h
//  OC基础学习
//
//  Created by 麦子 on 15/4/20.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import <Foundation/Foundation.h>

/******
     协议感觉就是java中的接口。 在这里,协议可以继承协议,(java中接口可以继承接口),只是在协议中是不可以定义实例变量。
 
     同时类目也可以采用协议。
 
     required: 必须实现的方法
 
     optional: 可选性的
 
     默认的事必须实现的。
 
     在代码编写中,加入  id<协议X>  这种形式定义为一个基类的属性的话,这样会使代码的高层更加的灵活。这也就是java中的一种多态的表现。
 
 
***/
@protocol PlayerProtocol <NSObject>

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);">@property<span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span>nonatomic<span style="font-variant-ligatures: no-common-ligatures; color: #000000">,</span>assign<span style="font-variant-ligatures: no-common-ligatures; color: #000000">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *teamName;  // 协议里面可以写属性,这样的属性在后面实现的这个协议的类中都可以调用,这样的方法也可以用来扩展公共属性。</span></p>
-(void) playGame;

@optional

-(void) goFishing;
-(void) gobar;



@end
//
//  BaskballPlayer.h
//  OC基础学习
//
//  Created by 麦子 on 15/4/20.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PlayerProtocol.h"

@interface BaskballPlayer : NSObject<PlayerProtocol>

@end

//
//  BaskballPlayer.m
//  OC基础学习
//
//  Created by 麦子 on 15/4/20.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "BaskballPlayer.h"

@implementation BaskballPlayer
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(187, 44, 162);">@synthesize<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> teamName;</span></p>
-(void) playGame
{<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">    self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span>teamName<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">骑士队</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(209, 47, 27);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span>@"<span style="font-family: 'Heiti SC Light';">球员需要打比赛。。。。打打打----</span>%@"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">,</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">teamName</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">);</span></p><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000">
</span></div>

}

@end

/***************协议************************/
        BaskballPlayer  *player = [[BaskballPlayer alloc] init];
        [player playGame];

2015-04-20 23:10:59.719 OC基础学习[1134:67744]球员需要打比赛。。。。打打打

Program ended with exit code: 0


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值