• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-C中的面向对象编程

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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即可。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-c——UI基础开发第六天(UITableView)发布时间:2022-07-12
下一篇:
objective-c中的@selector()和 c /c++的函数指针发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap