一、声明类接口步骤:

1、声明一个类接口,使用@interfacekeyword加上类名称。

2、用  { 实例变量 }  来定义各种数据成员。

3、方法声明,採用中缀符语法声明一个c函数,用到了冒号 : 。

二、声明类接口实例:

  1. //声明圆形circle类接口
  2. @interface Circle : NSObject
  3. {
  4. ShapeColor fillColor;//每次创建新的Circle对象后,对象中都包含这两个元素——类的实例变量
  5. ShapeRect bounds;
  6. }//指定实例变量
  7.  
  8. -(void) setFillColor: (ShapeColor) fillColor;//方法声明、中缀符
  9. -(void) setBounds: (ShapeRect) bounds;
  10. -(void) draw;
  11.  
  12. @end //Circle 完毕类的声明

三、实现类步骤

1、用keyword@implementation加上类名称

2、实现类中声明的方法,编写方法体 { 方法体 } 。

四、实现类实例

  1. @implementation Circle//类实现
  2.  
  3. -(void) setFillColor: (ShapeColor) c
  4. {
  5. fillColor = c;
  6. }//setFillColor
  7.  
  8. -(void) setBounds: (ShapeRect) b
  9. {
  10. bounds = b;
  11. }//setbounds
  12.  
  13. -(void) draw
  14. {
  15. NSLog(@"drawing a circle at(%d %d %d %d) in %@",
  16. bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)
  17. );
  18. }//draw
  19.  
  20. @end//Circle 完毕类的实现

五、案例练习——几何图形的绘制和填充颜色

  1. //
  2. // main.m
  3. // Shapes-Object
  4. //
  5. // Created by jason on 14-6-10.
  6. // Copyright (c) 2014年 JasonApp. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. //定义形状的不同颜色
  12. typedef enum{
  13. kRedColor,
  14. kGreenColor,
  15. kBlueColor
  16. } ShapeColor;
  17.  
  18. //定义一个矩形来指定屏幕上的绘制区域
  19. typedef struct{
  20. int x,y,width,height;
  21. } ShapeRect;
  22.  
  23. //负责转换传入的颜色值,并返回NSString字面量
  24. NSString *colorName(ShapeColor color)
  25. {
  26. switch (color) {
  27. case kRedColor:
  28. return @"Red";
  29. break;
  30.  
  31. case kGreenColor:
  32. return @"Green";
  33. break;
  34.  
  35. case kBlueColor:
  36. return @"Blue";
  37. break;
  38. }
  39.  
  40. return @"no clue";
  41.  
  42. }//colorName
  43.  
  44. //声明圆形circle类接口
  45. @interface Circle : NSObject
  46. {
  47. ShapeColor fillColor;//每次创建新的Circle对象后,对象中都包含这两个元素——类的实例变量
  48. ShapeRect bounds;
  49. }//指定实例变量
  50.  
  51. -(void) setFillColor: (ShapeColor) fillColor;//方法声明、中缀符
  52. -(void) setBounds: (ShapeRect) bounds;
  53. -(void) draw;
  54.  
  55. @end //Circle 完毕类的声明
  56.  
  57. @implementation Circle//类实现
  58.  
  59. -(void) setFillColor: (ShapeColor) c
  60. {
  61. fillColor = c;
  62. }//setFillColor
  63.  
  64. -(void) setBounds: (ShapeRect) b
  65. {
  66. bounds = b;
  67. }//setbounds
  68.  
  69. -(void) draw
  70. {
  71. NSLog(@"drawing a circle at(%d %d %d %d) in %@",
  72. bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor)
  73. );
  74. }//draw
  75.  
  76. @end//Circle 完毕类的实现
  77.  
  78. //定义矩形
  79. @interface Rectangle : NSObject
  80. {
  81. ShapeColor fillColor;
  82. ShapeRect bounds;
  83. }
  84.  
  85. - (void) setFillColor: (ShapeColor) fillColor;
  86.  
  87. - (void) setBounds: (ShapeRect) bounds;
  88.  
  89. - (void) draw;
  90.  
  91. @end // Rectangle
  92.  
  93. @implementation Rectangle
  94.  
  95. - (void) setFillColor: (ShapeColor) c
  96. {
  97. fillColor = c;
  98. } // setFillColor
  99.  
  100. - (void) setBounds: (ShapeRect) b
  101. {
  102. bounds = b;
  103. } // setBounds
  104.  
  105. - (void) draw
  106. {
  107. NSLog (@"drawing a rectangle at (%d %d %d %d) in %@",
  108. bounds.x, bounds.y,
  109. bounds.width, bounds.height,
  110. colorName(fillColor));
  111. } // draw
  112.  
  113. @end // Rectangle
  114.  
  115. //定义OblateSphereoids
  116. @interface OblateSphereoid : NSObject
  117. {
  118. ShapeColor fillColor;
  119. ShapeRect bounds;
  120. }
  121.  
  122. - (void) setFillColor: (ShapeColor) fillColor;
  123.  
  124. - (void) setBounds: (ShapeRect) bounds;
  125.  
  126. - (void) draw;
  127.  
  128. @end // OblateSphereoid
  129.  
  130. @implementation OblateSphereoid
  131.  
  132. - (void) setFillColor: (ShapeColor) c
  133. {
  134. fillColor = c;
  135. } // setFillColor
  136.  
  137. - (void) setBounds: (ShapeRect) b
  138. {
  139. bounds = b;
  140. } // setBounds
  141.  
  142. - (void) draw
  143. {
  144. NSLog (@"drawing an egg at (%d %d %d %d) in %@",
  145. bounds.x, bounds.y,
  146. bounds.width, bounds.height,
  147. colorName(fillColor));
  148. } // draw
  149.  
  150. @end // OblateSphereoid
  151.  
  152. //定义三角形
  153. @interface Triangle : NSObject
  154. {
  155. ShapeColor fillColor;
  156. ShapeRect bounds;
  157. }
  158.  
  159. - (void) setFillColor: (ShapeColor) fillColor;
  160.  
  161. - (void) setBounds: (ShapeRect) bounds;
  162.  
  163. - (void) draw;
  164.  
  165. @end // Triangle
  166.  
  167. @implementation Triangle
  168.  
  169. - (void) setFillColor: (ShapeColor) c
  170. {
  171. fillColor = c;
  172. } // setFillColor
  173.  
  174. - (void) setBounds: (ShapeRect) b
  175. {
  176. bounds = b;
  177. } // setBounds
  178.  
  179. - (void) draw
  180. {
  181. NSLog (@"drawing a triangle at (%d %d %d %d) in %@",
  182. bounds.x, bounds.y,
  183. bounds.width, bounds.height,
  184. colorName(fillColor));
  185. } // draw
  186.  
  187. @end // Triangle
  188.  
  189. //画形状
  190. void drawShapes (id shapes[], int count)
  191. {
  192. for (int i = 0; i < count; i++) {
  193. id shape = shapes[i];
  194. [shape draw];
  195. }
  196. }//drawShapes
  197.  
  198. int main(int argc, const char * argv[])
  199. {
  200.  
  201. id shapes[4];
  202.  
  203. ShapeRect rect0 = { 0, 0, 10, 30 };
  204. shapes[0] = [Circle new];
  205. [shapes[0] setBounds: rect0];
  206. [shapes[0] setFillColor: kRedColor];
  207.  
  208. ShapeRect rect1 = { 30, 40, 50, 60 };
  209. shapes[1] = [Rectangle new];
  210. [shapes[1] setBounds: rect1];
  211. [shapes[1] setFillColor: kGreenColor];
  212.  
  213. ShapeRect rect2 = { 15, 19, 37, 29 };
  214. shapes[2] = [OblateSphereoid new];
  215. [shapes[2] setBounds: rect2];
  216. [shapes[2] setFillColor: kBlueColor];
  217.  
  218. ShapeRect rect3 = { 47, 32, 80, 50 };
  219. shapes[3] = [Triangle new];
  220. [shapes[3] setBounds: rect3];
  221. [shapes[3] setFillColor: kRedColor];
  222.  
  223. drawShapes (shapes, 4);
  224.  
  225. return (0);
  226. }

六、小结

1、过程式编程——“函数第一,数据第二”

2、OOP——“数据第一,函数第二”

3、“开放/关闭原则”——软件实体应该对拓展开放、对改动关闭

4、objective-c中不存在private方法

5、objective-c执行时负责执行重要的任务,比方对象发送消息和传递參数等,以支持应用程序的执行

6、实例化对象——向对应的类发送new消息来创建对象。

【《Objective-C基础教程 》笔记ch03】(四)OC中的OOP的更多相关文章

  1. jQuery官方基础教程笔记(转载)

    本文转载于阮一峰的博文,内容基础,结构清晰,是jquery入门不可多得的资料,非常好,赞一个. 阮一峰:jQuery官方基础教程笔记 jQuery是目前使用最广泛的javascript函数库. 据统计 ...

  2. oc中的oop基础及类的基本介绍

    面向对象的(OOP)的基础知识 类(class):表示一组对象数据的结构体,对象通类来得到自身.类名首字母大写. 对象(objcet):是一种包含值和指向其类的隐藏指针的结构体.运行中的程序中通常会有 ...

  3. python基础教程笔记—即时标记(详解)

    最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...

  4. 阮一峰:jQuery官方基础教程笔记

    jQuery是目前使用最广泛的javascript函数库. 据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库. 对于网页开发者来 ...

  5. php基础教程笔记

    php的环境搭建很简单,从网上下载wamp service 2.5,官方网址http://www.wampserver.com/,有32位和64位的,必须下载跟系统一致的版本,不然会出现奇怪的错误,这 ...

  6. Cytoscape基础教程笔记

    昨天开始学用Cytoscape,其tutorial分为两个部分,基础的和高级 的.基础教程又分成了四课:Getting Started.Filters & Editor.Fetching Ex ...

  7. Java基础复习笔记系列 四 数组

    Java基础复习笔记系列之 数组 1.数组初步介绍? Java中的数组是引用类型,不可以直接分配在栈上.不同于C(在Java中,除了基础数据类型外,所有的类型都是引用类型.) Java中的数组在申明时 ...

  8. Python基础学习笔记(四)语句

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  9. python基础教程笔记—画幅好画(详解)

    今天写一下基础教程里面的第二个项目,主要使用python来做一个pdf的图,比较简单. 首先我们需要安装用到的模块pip install reportlab即可. 书上是用urlopen从往上下了一个 ...

  10. swift基础教程笔记

    http://www.imooc.com/learn/127 <玩儿转swift> 慕课网教程笔记,自己根据2.1的语法做了更新. I. 1.通过playground来学习.熟悉swift ...

随机推荐

  1. Oracle 函数 “申请通过后,将该表中循环遍历到的所有内容插到另一个表中”

    create or replace function mcode_apply_insert_material(p_mca_no VARCHAR2, p_action VARCHAR2, p_wf_no ...

  2. invalid byte sequence for encoding "UTF8": 0xe99d2c

    服务器还原数据库数据出错,老规矩... 字符集编码的问题 http://blog.csdn.net/beiigang/article/details/39582583 over....

  3. Unix IPC之Posix消息队列(3)

    struct mq_attr { long mq_flags; /* message queue flag : 0, O_NONBLOCK */ long mq_maxmsg; /* max numb ...

  4. RabbitMQ介绍及安装部署

    本节内容: RabbitMQ介绍 RabbitMQ运行原理 RabbitMQ重要术语 三种ExchangeType RabbitMQ集群种类 集群基本概念 镜像模式部署集群 一.RabbitMQ介绍 ...

  5. HBase(七)Hbase过滤器

    一.过滤器(Filter) 基础API中的查询操作在面对大量数据的时候是非常苍白的,这里Hbase提供了高级的查询方法:Filter.Filter可以根据簇.列.版本等更多的条件来对数据进行过滤,基于 ...

  6. day7 面向对象class()学习

        面向过程 VS 面向对象     编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓 ...

  7. 浅谈Comparable与Comparator的区别

    平时进行自定义排序一直使用实现Comparable接口,一段时间后操作的时候居然发现有了个Comparator接口 上网差了些资料,总结笔记一下. 基本原理就是比较,底层是二叉树 比如是3,6,5,1 ...

  8. [漏洞复现]CVE-2018-4887 Flash 0day

    1.漏洞概述 2018年2月1号,Adobe官方发布安全通报(APSA18-01),声明Adobe Flash 28.0.0.137及其之前的版本,存在高危漏洞(CVE-2018-4878). 攻击者 ...

  9. 某gov的逻辑漏洞

    首先找一个号 在企业信息里面查看到大量的企业名称和组织机构代码 随后去找回密码那 可以看到是直接显示了用户名和密码 随后去登录 可以看到大量的工程信息个企业注册信息 ​

  10. Go Web编程 第三章--接收请求

    net/http标准库 net/http标准库通常包括两个部分,客户端和服务器,我们可以通过ListenAndServe创建一个简陋的服务器 package main import ( "n ...