protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能。Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充

Objective-C单继承的缺陷,协议并不是一个真正的类,在协议的里面只声明方法不实现,并且在协议当中不能声明实例变量,如果一个类实现了某一个协议的方法,那么称折各类遵

循(遵守,采用)了这个协议,正式的协议在类中必须被实现,一个类可以实现多个协议,一个协议也可以被多个类实现,

格式 format:

@protocol protocol_name<protocol,....>

required_Method_Declarations //默认是required  在类中必须被实现

@optional

optional_Method_Declarations // 在类中可以选择性的实现,

@required

required_Method_Declarations  //在类中必须被实现,

@end

//  MyPoint.h

#import <Foundation/Foundation.h>
//xieyi 协议 protocol
@protocol showOn
@required
-(void)printOn;
@end
// lei
@interface MyPoint : NSObject<showOn,NSCopying>{
int x;
int y;
}
@property (nonatomic,assign)int x,y;
-(id)initWithX:(int)_x andY:(int)_y;
@end
// leibie fenlei category 分类
@interface MyPoint(MoveSet) -(void)moveToX:(int)_x andY:(int)_y; @end

  

//  MyPoint.m

#import "MyPoint.h"

@implementation MyPoint
@synthesize x,y;
-(id)initWithX:(int)_x andY:(int)_y{
if(_x==0)
x=1;
if(_y==0)
y=1;
x=_x;
y=_y;
return self;
}
//copyWithZone 方法,避免浅拷贝
-(id)copyWithZone:(NSZone *)zone{
MyPoint* newPoint=[[MyPoint allocWithZone:zone] initWithX:x andY:y];
return newPoint;
}
-(void)printOn{
NSLog(@" x = %d , y = %d ",x,y);
}
@end
@implementation MyPoint(MoveSet)
-(void)moveToX:(int)_x andY:(int)_y{
x=_x;
y=_y;
}
@end

  

//  Circle.h

#import <Foundation/Foundation.h>
#import "MyPoint.h" @interface Circle : NSObject<showOn>{
MyPoint* point;
int radius;
}
@property (nonatomic,copy)MyPoint* point;
@property (nonatomic,assign)int radius;
-(id)initWithPoint:(MyPoint* )_p andRadius:(int)_r;
-(id)initWithPoint:(MyPoint *)_p ;
@end

  

//  Circle.m

#import "Circle.h"

@implementation Circle
@synthesize point;
@synthesize radius;
//重写初始化方法
-(id)initWithPoint:(MyPoint *)_p andRadius:(int)_r{
if(_r==0)
{
radius=1;
}
if(_p==nil)
return nil;
if(self=[super init])
{
if(point!=_p)
{
if(point)
[point release];
point =[[MyPoint alloc]initWithX:_p.x andY:_p.y];
} }
radius=_r;
return self;
}
//重写初始化方法,
-(id)initWithPoint:(MyPoint*)_p{
self=[self initWithPoint:_p andRadius:10];
return self;
}
//实现协议中的方法。
-(void)printOn{
NSLog(@"point(x,y) = (%d,%d),radius = %d",point.x,point.y,radius);
}
@end

  

Objective-C /iphone开发基础:协议(protocol)的更多相关文章

  1. [置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)

    protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能.Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充 ...

  2. 浅谈iOS开发的协议(protocol)和代理(delegate)

    协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...

  3. Objective-C /iphone开发基础:分类(category,又称类别)

    在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...

  4. [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)

    在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...

  5. [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明

    协议是为了弥补Objective-c中类只能单继承的缺陷,在Objective-c2.0之前当一个类遵循一个协议的时候,必须在类中实现协议的所有方法,在Objective-c2.0之后协议中的方法就有 ...

  6. Ios开发之协议protocol

    Protocol是ios开发中的一个难点也是一个重点,要想使用好,或者理解好它,可能需要时间的累积.今天我们就通过一个例子来简单的看一下,怎么样使用protocol. 我们今天用的例子就是模拟电脑插入 ...

  7. 深入理解iPhone数据持久化(手把手教你iphone开发 – 基础篇)

    在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保存在相应的磁盘目录中和数据库中.symbian中因为权限认证的原因,在3rd上大多 ...

  8. Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)

    第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试,现在用不着. 点击next  ->creat之 ...

  9. iPhone开发基础教程_第二章

    1.各个子文件夹的作用        Classes:                    编写的大多代码都保存在这里,其中包括所有的Objective-C类,可以在Classes文件夹下创建一些子 ...

随机推荐

  1. HDU4614 Vases and Flowers 二分+线段树

    分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...

  2. 【原】Storm 消息处理保障机制

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  3. Solution for latex error:”Unknown graphics extension: .eps“ or "Can not find XXX"

    Sample code: \begin{figure*} \centering % Requires \usepackage{graphicx} \includegraphics[width=7in] ...

  4. HW6.20

    public class Solution { public static void main(String[] args) { int[][] chessboard = new int[8][8]; ...

  5. HW6.18

    public class Solution { public static void main(String[] args) { double[] array = {6.0, 4.4, 1.9, 2. ...

  6. light oj 1078 - Integer Divisibility

    1078 - Integer Divisibility   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  7. [OC Foundation框架 - 22] 集合的内存管理

    A.集合的手动内存管理 NSArray addObject: 加入的元素执行一次retain removeObject: 被删除的元素执行一次release removeAllObjects: 所有元 ...

  8. (一)Bootstrap简介

    Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的. Bootstrap优点: 移动设备优先:自 Boot ...

  9. Learn_Dynamic

    首先看一下Dynamic的定义 Visual C# 2010 引入了一个新类型 dynamic. 该类型是一种静态类型,但类型为 dynamic 的对象会跳过静态类型检查. 大多数情况下,该对象就像具 ...

  10. android应用程序fps meter[帧数显示]的分析 —— 浅谈root的风险 (3)

    上节已经详细说了下注入过程,最后寄生进程在宿主进程中下了个蛋,这下完的蛋有什么作用呢?接下来再具体分析一下. lib0的感染过程分析 对于本例注入的so动态库,首先看一下so的符号: $ readel ...