//
// 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