博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
精通Objective-C系列 - 3) 类的申明和实现
阅读量:6614 次
发布时间:2019-06-24

本文共 4154 字,大约阅读时间需要 13 分钟。

如果学过Java和C#的人,这里容易搞混,

Objective-C中的@interface关键字是申明类

@interface Circle : NSObject{    @private    ShapeColor fillColor;    ShapeRect bounds;}- (void) setFillColor: (ShapeColor) fillColor;- (void) setBounds: (ShapeRect) bounds;- (void) draw;@end // Circle

如上,这样的申明类的方式确实让我们Java和C#程序员有点不太习惯,连缩进方式也是

还是来看一下完整代码吧

#import 
// --------------------------------------------------// constants for the different kinds of shapes and their colorstypedef enum { kRedColor, kGreenColor, kBlueColor} ShapeColor;// --------------------------------------------------// Shape bounding rectangletypedef struct { int x, y, width, height;} ShapeRect;// --------------------------------------------------// convert from the ShapeColor enum value to a human-readable nameNSString *colorName (ShapeColor color){ switch (color) { case kRedColor: return @"red"; break; case kGreenColor: return @"green"; break; case kBlueColor: return @"blue"; break; } return @"no clue"; } // colorName// --------------------------------------------------// All about Circles@interface Circle : NSObject{ ShapeColor fillColor; 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// --------------------------------------------------// All about Rectangles@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// --------------------------------------------------// All about 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// --------------------------------------------------// Draw the shapesvoid drawShapes (id shapes[], int count){ for (int i = 0; i < count; i++) { id shape = shapes[i]; [shape draw]; } } // drawShapes// --------------------------------------------------// The main function. Make the shapes and draw themint main (int argc, const char * argv[]) { id shapes[3]; 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]; drawShapes (shapes, 3); return (0); } // main

运行结果:

drawing a circle at (0 0 10 30) in red

drawing a rectangle at (30 40 50 60) in green
drawing an egg at (15 19 37 29) in blue
drawing a triangle at (47 32 80 50) in red

 

 

转载于:https://www.cnblogs.com/davidgu/archive/2013/02/20/2918225.html

你可能感兴趣的文章
可能是把Java内存区域讲的最清楚的一篇文章
查看>>
Ionic3开发环境搭建-VS Code
查看>>
Docker 搭建私有仓库
查看>>
django 1.8 官方文档翻译: 3-2-3
查看>>
有一种娱乐叫看别人编程
查看>>
phar反序列化
查看>>
谷歌:光靠软件修复不能完全防御“幽灵”漏洞
查看>>
两种使用原生js实现导出excel数据简单讲解
查看>>
Github使用教程
查看>>
【朝花夕拾】Android性能篇之(三)Java内存回收
查看>>
KDE Plasma 5.15.3 发布,Plasma 桌面环境
查看>>
说说你对执行 JS 代码时产生的执行上下文的理解?
查看>>
Confluence 6 访问日志脚本
查看>>
安卓手机如何在桌面上添加敬业签提醒便签?
查看>>
大讲堂 | 人工智能技术新浪潮浅析
查看>>
Redis · 引擎特性 · 基于 LFU 的热点 key 发现机制
查看>>
《Linux命令行与shell脚本编程大全》第十六章 控制脚本
查看>>
获取浏览器滚动距离
查看>>
小程序获取用户信息
查看>>
利用python构建一个简单的推荐系统
查看>>