zoukankan      html  css  js  c++  java
  • Objective-C语言分类与协议

    分类(Category)允许向一个类文件中添加新的方法声明,它不需要使用子类机制,并且在类实现的文件中的同一个名字下定义这些方法。其语法举例如下:

    #import "ClassName.h"
    @interface ClassName ( CategoryName )
    // 方法声明
    @end

    前面多态性中曾经使用过Vector和Scalar的例子,下面我们为Vector增加“减”sub的方法。

    #import <Foundation/Foundation.h>
    #import "Vector.h"
    @interface Vector (sub)
    -(Vector *) sub: (Vector *) v;
    @end
    #import "Vector+sub.h"
    @implementation Vector (sub)
    -(Vector *) sub: (Vector *) v {
    Vector *result = [[Vector alloc] init];
    [result setVec1: vec1 - [v vec1]
    andVec2: vec2 - [v vec2]];
    return result;
    }
    @end
    #import <Foundation/Foundation.h>
    #import "Vector+sub.h"
    int main (int argc, const char * argv[]) {
    Vector *vecA =[[Vector alloc] init];
    Vector *vecB =[[Vector alloc] init];
    id result;
    //set the values
    [vecA setVec1: 3.2 andVec2: 4.7];
    [vecB setVec1: 32.2 andVec2: 47.7];
    // print it
    [vecA print];
    NSLog(@" + ");
    [vecB print];
    NSLog(@" = ");
    result = [vecA add: vecB];
    [result print];
    ... ...
    [vecA print];
    NSLog(@" - ");
    [vecB print];
    NSLog(@" = ");
    result = [vecA sub: vecB];
    [result print];
    // free memory
    [vecA release];
    [vecB release];
    [result release];
    return 0;
    }

    其中result = [vecA add: vecB]中的add:是Vector类原有的方法,result = [vecA sub: vecB]中都sub:是Vector分类添加的方法。分类是在Java和C++等面向对象的语言中没有的概念,分类本质上是通过Objective-C的动态绑定而实现的。通过分类使用能够达到比继承更好的效果。

    • 协议(Protocol)与Java的Interface(接口) 或者 C++的纯虚类相同,就是用来声明接口的。协议只是定义了方法的列表,协议不负责实现方法,目的是让别的类来实现。

    • Graphics中定义了onDraw方法,但是我们仔细分析一下onDraw方法不能实现的,作为Graphics(几何图形)它无法知道它的子类如何绘制图形,它只能规定绘制图名字为onDraw签名和返回值等信息,但不能给出具体的实现,因此Graphics(几何图形)不应该设计成为类而应该设计成为协议。

     多个协议用,分开

    @protocol Graphics
    -(void) onDraw;
    @end

    • 协议只有接口部分,没有实现部分,所以没有m文件,关键字@protocol,协议可以继承别的协议,协议中不能定义成员变量。

    #import <Foundation/Foundation.h>
    #import "Graphics.h"
    @interface Ellipse:NSObject <Graphics> {
    }
    @end
    #import "Ellipse.h”
    @implementation Ellipse
    -(void)onDraw {
    NSLog(@"绘制椭圆形");
    }
    @end
    #import <Foundation/Foundation.h>
    #import “Triangle.h"
    @interface Triangle:NSObject <Graphics> {
    }
    @end
    #import "Triangle.h”
    @implementation Triangle
    -(void)onDraw {
    NSLog(@"绘制三角形");
    }
    @end

    • 协议的实现是在类声明的父类之后,加上<Graphics>,与类的单个继承不同,协议可以实现多个,表示要实现这个协议,如果有多个协议要实现用“,”号分隔:<P1,P2>

    #import <Foundation/Foundation.h>
    #import "Graphics.h"
    #import "Ellipse.h"
    #import "Triangle.h"
    int main (int argc, const char * argv[]) {
    id graphics;
    graphics = [[Ellipse alloc] init];
    [graphics onDraw];
    [graphics release];
    graphics = [[Triangle alloc] init];
    [graphics onDraw];
    [graphics release];
    return 0;
    }

     两个协议重要的协议

    • NSCopying //对象复制(克隆)
    • NSCoding //对象可序列号
    @protocol MyProtocol
    - (void)requiredMethod;//必须实现的方法
    @optional//可选的方法
    - (void)anOptionalMethod;
    - (void)anotherOptionalMethod;
    @required//必须实现的方法
    - (void)anotherRequiredMethod;
    @end
  • 相关阅读:
    其他
    Win10
    Win10
    面向对象与设计模式
    Git
    Java
    Git
    Git
    Git
    一、I/O操作(File文件对象)
  • 原文地址:https://www.cnblogs.com/ecollab/p/6124365.html
Copyright © 2011-2022 走看看