一、声明类接口步骤:
1、声明一个类接口,使用@interface关键字加上类名称。
2、用 { 实例变量 } 来定义各种数据成员。
3、方法声明,采用中缀符语法声明一个c函数,用到了冒号 : 。
二、声明类接口实例:
//声明圆形circle类接口 @interface Circle : NSObject { ShapeColor fillColor;//每次创建新的Circle对象后,对象中都包括这两个元素——类的实例变量 ShapeRect bounds; }//指定实例变量 -(void) setFillColor: (ShapeColor) fillColor;//方法声明、中缀符 -(void) setBounds: (ShapeRect) bounds; -(void) draw; @end //Circle 完成类的声明
三、实现类步骤
1、用关键字@implementation加上类名称
2、实现类中声明的方法,编写方法体 { 方法体 } 。
四、实现类实例
@implementation Circle//类实现 -(void) setFillColor: (ShapeColor) c { fillColor = c; }//setFillColor -(void) setBounds: (ShapeRect) b { bounds = b; }//setbounds -(void) draw { NSLog(@"drawing a circle at(%d %d %d %d) in %@", bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor) ); }//draw @end//Circle 完成类的实现
五、案例练习——几何图形的绘制和填充颜色
// // main.m // Shapes-Object // // Created by jason on 14-6-10. // Copyright (c) 2014年 JasonApp. All rights reserved. // #import <Foundation/Foundation.h> //定义形状的不同颜色 typedef enum{ kRedColor, kGreenColor, kBlueColor } ShapeColor; //定义一个矩形来指定屏幕上的绘制区域 typedef struct{ int x,y,width,height; } ShapeRect; //负责转换传入的颜色值,并返回NSString字面量 NSString *colorName(ShapeColor color) { switch (color) { case kRedColor: return @"Red"; break; case kGreenColor: return @"Green"; break; case kBlueColor: return @"Blue"; break; } return @"no clue"; }//colorName //声明圆形circle类接口 @interface Circle : NSObject { ShapeColor fillColor;//每次创建新的Circle对象后,对象中都包括这两个元素——类的实例变量 ShapeRect bounds; }//指定实例变量 -(void) setFillColor: (ShapeColor) fillColor;//方法声明、中缀符 -(void) setBounds: (ShapeRect) bounds; -(void) draw; @end //Circle 完成类的声明 @implementation Circle//类实现 -(void) setFillColor: (ShapeColor) c { fillColor = c; }//setFillColor -(void) setBounds: (ShapeRect) b { bounds = b; }//setbounds -(void) draw { NSLog(@"drawing a circle at(%d %d %d %d) in %@", bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor) ); }//draw @end//Circle 完成类的实现 //定义矩形 @interface Rectangle : NSObject { ShapeColor fillColor; ShapeRect bounds; } - (void) setFillColor: (ShapeColor) fillColor; - (void) setBounds: (ShapeRect) bounds; - (void) draw; @end // Rectangle @implementation Rectangle - (void) setFillColor: (ShapeColor) c { fillColor = c; } // setFillColor - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { NSLog (@"drawing a rectangle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); } // draw @end // Rectangle //定义OblateSphereoids @interface OblateSphereoid : NSObject { ShapeColor fillColor; ShapeRect bounds; } - (void) setFillColor: (ShapeColor) fillColor; - (void) setBounds: (ShapeRect) bounds; - (void) draw; @end // OblateSphereoid @implementation OblateSphereoid - (void) setFillColor: (ShapeColor) c { fillColor = c; } // setFillColor - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { NSLog (@"drawing an egg at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); } // draw @end // OblateSphereoid //定义三角形 @interface Triangle : NSObject { ShapeColor fillColor; ShapeRect bounds; } - (void) setFillColor: (ShapeColor) fillColor; - (void) setBounds: (ShapeRect) bounds; - (void) draw; @end // Triangle @implementation Triangle - (void) setFillColor: (ShapeColor) c { fillColor = c; } // setFillColor - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { NSLog (@"drawing a triangle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); } // draw @end // Triangle //画形状 void drawShapes (id shapes[], int count) { for (int i = 0; i < count; i++) { id shape = shapes[i]; [shape draw]; } }//drawShapes int main(int argc, const char * argv[]) { id shapes[4]; ShapeRect rect0 = { 0, 0, 10, 30 }; shapes[0] = [Circle new]; [shapes[0] setBounds: rect0]; [shapes[0] setFillColor: kRedColor]; ShapeRect rect1 = { 30, 40, 50, 60 }; shapes[1] = [Rectangle new]; [shapes[1] setBounds: rect1]; [shapes[1] setFillColor: kGreenColor]; ShapeRect rect2 = { 15, 19, 37, 29 }; shapes[2] = [OblateSphereoid new]; [shapes[2] setBounds: rect2]; [shapes[2] setFillColor: kBlueColor]; ShapeRect rect3 = { 47, 32, 80, 50 }; shapes[3] = [Triangle new]; [shapes[3] setBounds: rect3]; [shapes[3] setFillColor: kRedColor]; drawShapes (shapes, 4); return (0); }
六、小结
1、过程式编程——“函数第一,数据第二”
2、OOP——“数据第一,函数第二”
3、“开放/关闭原则”——软件实体应该对拓展开放、对修改关闭
4、objective-c中不存在private方法
5、objective-c运行时负责执行重要的任务,比如对象发送消息和传递参数等,以支持应用程序的运行
6、实例化对象——向相应的类发送new消息来创建对象。