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 ,/,ios,/iphone开发基础:协议(protocol)的更多相关文章

  1. [置顶] 提高生产力:Web开发基础平台WebCommon的设计和实现

    Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...

  2. [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

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

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

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

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

  5. [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,

    在x-code Version 4.3.2 (4E2002)下编译: 在 Controller. m 文件下添加如下实例方法即可: - (void)viewDidUnload { [super vie ...

  6. [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton

    第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...

  7. [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

  8. [置顶] Objective-C ,ios,iphone开发基础:命名规范

    命名规范:http://bukkake.iteye.com/blog/695492  点击打开链接

  9. Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)

    使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...

随机推荐

  1. Codeforces Round #262 (Div. 2) 460C. Present(二分)

    题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...

  2. IIS7.0+SqlServer2012,进行.net网站发布的安装全过程

    1..net3.5安装(sqlserver2012需要) 控制面板-->管理工具-->服务器管理器-->功能-->添加功能-->选择".NET Framewor ...

  3. javascript 生成页面轮播元素

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    ...

  4. BZOJ 1800 fly-飞行棋

           这道题其实考察的就是从其中能找到几条直径,因为这次数据范围比较小,所以只需设一个二维数组,记录一下每个点及每个点从零开始的位置,最后定一个变量记录周长,最后用个循环搜一下位置小于周长一半 ...

  5. BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )

    可并堆水题 --------------------------------------------------------- #include<bits/stdc++.h>   usin ...

  6. ContentProvider中的数据库的生成时机以及ContentResolver的insert()方法总结

    经过几天的总结,以及结合一些代码的实际测试,终于算是明白了ContentProvider中的数据的生成时机了. 目录结构: MainActivity.java package com.wyl.cont ...

  7. 安装 unixbench 报错解决方法

    一.准备工作 1.首先使用root用户登陆. 2.运行Unixbeanch需要GCC的支持,在安装Unixbeanch之前,需要先安装GCC,在Debian中,直接执行如下命令: 复制代码 代码如下: ...

  8. 高质量程序设计指南C/C++语言——C++/C程序设计入门(3)

  9. 网站开启gzip的方法

    .如果空间支持Zlib压缩文件,可用php.ini方法 这个方法比较简单,压缩率也较高,可达75%左右.新建一个名为 php.ini 的文件,添加以下代码,保存后上传至网站根目录即可. output_ ...

  10. HDU1005(周期问题)

    Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...