[置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)
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)的更多相关文章
- [置顶] 提高生产力:Web开发基础平台WebCommon的设计和实现
		
Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...
 - [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解
		
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
 - [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明
		
协议是为了弥补Objective-c中类只能单继承的缺陷,在Objective-c2.0之前当一个类遵循一个协议的时候,必须在类中实现协议的所有方法,在Objective-c2.0之后协议中的方法就有 ...
 - [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)
		
在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...
 - [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
		
在x-code Version 4.3.2 (4E2002)下编译: 在 Controller. m 文件下添加如下实例方法即可: - (void)viewDidUnload { [super vie ...
 - [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton
		
第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...
 - [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
		
SQLite 是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...
 - [置顶] Objective-C ,ios,iphone开发基础:命名规范
		
命名规范:http://bukkake.iteye.com/blog/695492 点击打开链接
 - Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
		
使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...
 
随机推荐
- float 覆盖元素的问题
			
<span class="right-span"><a href="/xxx/" class="btn">增加Ser ...
 - iOS开发中地图开发的简单应用
			
iOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可.这次要实现的效果如下: 有标注(大头针),定位,地图. 1.添加地图 1.1 新一个Single V ...
 - Android ImageView(scaleType属性)图片按比例缩放
			
<ImageView android:id="@+id/img" android:src="@drawable/logo" android:scaleTy ...
 - 在springmvc中controller的一个方法处理多个不同请求
			
value的uri值为以下三类: A) 可以指定为普通的具体值: B) 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables): @Req ...
 - CMake初步(1)
			
转自:<你所不知的OSG>第一章:CMake初步(1)http://bbs.osgchina.org/forum.php?mod=viewthread&tid=1189&f ...
 - Eclipse3.6 添加JUnit源代码
			
Eclipse中无法查看JUnit源代码,也无法设置源代码的jar. 解决方法: 1. 下载org.junit.source_4.8.1.v4_8_1_v20100427-1100.jar,放到ec ...
 - SGU 106 The Equation 扩展欧几里得应用
			
Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...
 - java--内部类访问final成员
			
局部类只能访问外包方法中的final成员.位于方法内部的局部类,可以访问局部类之外,外包方法之内的所以变量和方法,但是生命周期不同,延长生命周期的办法就是将变量设置为final类型. 1)从程序设计语 ...
 - Java基础02 方法与数据成员
			
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在Java基础01 从HelloWorld到面向对象,我们初步了解了对象(obje ...
 - python 读取图片的尺寸、分辨率
			
#需要安装PIL模块 #encoding=gbk#--------------------------------------------------------------------------- ...