码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-C中的面向对象编程

时间:2016-04-06 00:38:20      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

1.过程式编程实例,画出Shape数组中的图形:

//
//  main.m
//  hello-obj
//
//  Created by zhouyang on 16/4/4.
//  Copyright © 2016年 zhouyang. All rights reserved.
//
#import <Foundation/Foundation.h>
//枚举变量,代表形状
typedef enum{
    kCircle,
    kRectangle,
    kEgg
} ShapeType;
//枚举变量,代表颜色
typedef enum{
    kRedColor,
    kGreenColor,
    kBlueColor
} ShapeColor;
//结构体,代表形状
typedef struct{
    int x,y,width,height;
}ShapeRect;
//结构体,表示多边形的各项属性
typedef struct {
    ShapeType type;
    ShapeColor fillColor;
    ShapeRect bounds;
    
}Shape;
//返回颜色对应的NSString
NSString *colorName(ShapeColor colorName){
    switch(colorName){
        case kRedColor:
            return @"red";
            break;
        case kGreenColor:
            return @"green";
            break;
        case kBlueColor:
            return @"blue";
            break;
    }
    return @"no clue";
}
//“画出”圆,其实就是输出图形的各项属性
void drawCircle(ShapeRect bounds,ShapeColor fillColor){
    NSLog(@"drawing a circle at (%d,%d,%d,%d) in %@",
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          colorName(fillColor)
    );
}
//“画出”长方形
void drawRectangle(ShapeRect bounds,ShapeColor fillColor){
    NSLog(@"drawing a rectangle at (%d,%d,%d,%d) in %@",
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          colorName(fillColor)
    );
}
//"画出"Egg
void drawEgg(ShapeRect bounds,ShapeColor fillColor){
    NSLog(@"drawing a egg at (%d,%d,%d,%d) in %@",
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          colorName(fillColor)
    );
}
//遍历Shape数组,画出对应的图形
void drawShapes(Shape shapes[],int count){
    for(int i=0;i<count;i++){
        switch (shapes[i].type) {
            case kCircle:
                drawCircle(shapes[i].bounds, shapes[i].fillColor);
                break;
            case kRectangle:
                drawRectangle(shapes[i].bounds,shapes[i].fillColor);
                break;
            case kEgg:
                drawEgg(shapes[i].bounds,shapes[i].fillColor);
                break;
                
            default:
                NSLog(@"can‘t find the shape!");
        }
        
    }
}
int main(int argc, const char * argv[]) {
    Shape shapes[3];
    //Circle
    ShapeRect rect0={0,0,10,30};
    shapes[0].type=kCircle;
    shapes[0].fillColor=kRedColor;
    shapes[0].bounds=rect0;
    //Rectangle
    ShapeRect rect1={30,40,50,60};
    shapes[1].type=kRectangle;
    shapes[1].fillColor=kGreenColor;
    shapes[1].bounds=rect1;
    //Egg
    ShapeRect rect2={15,18,37,29};
    shapes[2].type=kEgg;
    shapes[2].fillColor=kBlueColor;
    shapes[2].bounds=rect2;
    //画出数组Shape中的图形
    drawShapes(shapes,3);
}

  基于OOP编程,可以将上述代码改写为以下格式:

#import <Foundation/Foundation.h>
//注意枚举变量的定义,变量之间以逗号隔开
typedef enum{
    kRedColor,
    kGreenColor,
    kBlueColor
}ShapeColor;
typedef struct {
    int x,y,width,height;
}ShapeRect;
NSString *ColorName(ShapeColor color){
    switch (color) {
        case kRedColor:
            return @"red";
            break;
        case kGreenColor:
            return @"green";
            break;
        case kBlueColor:
            return @"blue";
            break;
    }
     return @"no clue";
}
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Circle:NSObject{
    //定义数据成员
    @private
    ShapeColor fillColor;
    ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Circle
//接口定义完毕
//下面开始实现接口Circle
@implementation Circle
- (void) setFillColor:(ShapeColor) c {
    fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
    bounds=b;
}
- (void) draw{
    NSLog(@"drawing a circle at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          ColorName(fillColor)
          );
}
@end //Circle
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Rectangle:NSObject{
    //定义数据成员
@private
    ShapeColor fillColor;
    ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Rectangle
//接口定义完毕
//下面开始实现接口Rectangle
@implementation Rectangle
- (void) setFillColor:(ShapeColor) c {
    fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
    bounds=b;
}
- (void) draw{
    NSLog(@"drawing a rectangle at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          ColorName(fillColor)
          );
}
@end //Egg
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Egg:NSObject{
    //定义数据成员
@private
    ShapeColor fillColor;
    ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Triangle
//接口定义完毕
//下面开始实现接口Triangle
@implementation Egg
- (void) setFillColor:(ShapeColor) c {
    fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
    bounds=b;
}
- (void) draw{
    NSLog(@"drawing a egg at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          ColorName(fillColor)
          );
}
@end //Egg
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Triangle:NSObject{
    //定义数据成员
@private
    ShapeColor fillColor;
    ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Triangle
//接口定义完毕
//下面开始实现接口Triangle
@implementation Triangle
- (void) setFillColor:(ShapeColor) c {
    fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
    bounds=b;
}
- (void) draw{
    NSLog(@"drawing a triangle at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          ColorName(fillColor)
          );
}
@end //Triangle
//画出图形
//id 对应类实例
void drawShapes(id shapes[],int count){
    for(int i=0;i<count;i++){
        id shape=shapes[i];
        [shape draw];
    }
}
int main(int argc, const char * argv[]) {
    
    id shapes[4];
    //Circle
    shapes[0]=[Circle new];//这里创建新的类实例将new加在类名称的后面
    ShapeRect rect0={0,0,10,30};
    [shapes[0] setBounds:rect0];
    [shapes[0] setFillColor:kRedColor];
    //Rectangle
    ShapeRect rect1={30,40,50,60};
    shapes[1] =[ Rectangle new];
    [shapes[1] setBounds:rect1];
    [shapes[1] setFillColor:kGreenColor];
    //Egg
    ShapeRect rect2={15,18,37,29};
    shapes[2] =[Egg new];
    [shapes[2] setBounds:rect2];
    [shapes[2] setFillColor:kBlueColor];
    //Triangle
    ShapeRect rect3 ={47,32,80,50};
    shapes[3] =[Triangle new];
    [shapes[3] setBounds:rect3];
    [shapes[3] setFillColor:kGreenColor];
    //画出数组Shape中的图形
    drawShapes(shapes,4);
    return 0;
}

    可见利用OOP编程方式,增加新的图形更加简单,修改的地方更少。

    请注意object-c中的接口,类的定义以及类方法的调用,这里与C++还是存在很多的不同。

    可能会遇到错误:Must explicitly describe intended ownership of an object array parameter,解决方法如下:

 技术分享 

    将Objective-c Automatic Reference Counting 设置为No,关闭ARC即可。

 

Objective-C中的面向对象编程

标签:

原文地址:http://www.cnblogs.com/zhoudayang/p/5357554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!