【《Objective-C基础教程 》笔记ch03】(四)OC中的OOP
一、声明类接口步骤:
1、声明一个类接口,使用@interfacekeyword加上类名称。
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、用keyword@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消息来创建对象。
【《Objective-C基础教程 》笔记ch03】(四)OC中的OOP的更多相关文章
- jQuery官方基础教程笔记(转载)
本文转载于阮一峰的博文,内容基础,结构清晰,是jquery入门不可多得的资料,非常好,赞一个. 阮一峰:jQuery官方基础教程笔记 jQuery是目前使用最广泛的javascript函数库. 据统计 ...
- oc中的oop基础及类的基本介绍
面向对象的(OOP)的基础知识 类(class):表示一组对象数据的结构体,对象通类来得到自身.类名首字母大写. 对象(objcet):是一种包含值和指向其类的隐藏指针的结构体.运行中的程序中通常会有 ...
- python基础教程笔记—即时标记(详解)
最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...
- 阮一峰:jQuery官方基础教程笔记
jQuery是目前使用最广泛的javascript函数库. 据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库. 对于网页开发者来 ...
- php基础教程笔记
php的环境搭建很简单,从网上下载wamp service 2.5,官方网址http://www.wampserver.com/,有32位和64位的,必须下载跟系统一致的版本,不然会出现奇怪的错误,这 ...
- Cytoscape基础教程笔记
昨天开始学用Cytoscape,其tutorial分为两个部分,基础的和高级 的.基础教程又分成了四课:Getting Started.Filters & Editor.Fetching Ex ...
- Java基础复习笔记系列 四 数组
Java基础复习笔记系列之 数组 1.数组初步介绍? Java中的数组是引用类型,不可以直接分配在栈上.不同于C(在Java中,除了基础数据类型外,所有的类型都是引用类型.) Java中的数组在申明时 ...
- Python基础学习笔记(四)语句
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...
- python基础教程笔记—画幅好画(详解)
今天写一下基础教程里面的第二个项目,主要使用python来做一个pdf的图,比较简单. 首先我们需要安装用到的模块pip install reportlab即可. 书上是用urlopen从往上下了一个 ...
- swift基础教程笔记
http://www.imooc.com/learn/127 <玩儿转swift> 慕课网教程笔记,自己根据2.1的语法做了更新. I. 1.通过playground来学习.熟悉swift ...
随机推荐
- 洛谷P3203弹飞绵羊
传送门啦 非常神奇的分块大法. 每块分 √N 个元素 , 预处理出来:对于每个点,记录两个量:一个是它要弹几次才能出它所在的这个块,另外一个是它弹出这个块后到哪个点. 查询操作:一块一块跳过去 单次复 ...
- Java I/O系列汇总
1.Java I/O---概述 2.Java I/O---File类 3.Java I/O---获取文件目录并写入到文本 4.Java I/O---输入与输出 5.Java I/O---复制文本文件 ...
- 浅谈C、C++及其区别、兼容与不兼容
一.闲说C C语言之所以命名为C,是因为C语言源自Ken Thompson发明的B语言,而 B语言则源自BCPL语言. 1967年,剑桥大学的Martin Richards对CPL语言进行了简化,于是 ...
- Pytest里,mark装饰器的使用,双引号,没引号,这种差别很重要
按最新版的pytest测试框架. 如果只是单一的mark,不要加任何引号. 如果是要作and ,not之类的先把,一定要是双引号! 这个要记清楚,好像和以前版本的书上介绍的不一样,切记! import ...
- spring-boot分环境打包为tar包
1.pom配置 <!-- 多环境打包 start --> <profiles> <!-- 开发环境配置 --> <profile> <id> ...
- C语言:奇偶归一猜想
1.奇偶归一猜想——求多少步归一.(10分) 题目内容: 奇偶归一猜想——对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1. 如n = 11,得 ...
- USACO 6.5 The Clocks
The ClocksIOI'94 - Day 2 Consider nine clocks arranged in a 3x3 array thusly: |-------| |-------| |- ...
- USACO 6.2 Calf Flac
Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty la ...
- centos6 yum 安装nginx 不成功解决办法
转自 http://wlheihei.com/view/64 [root@51ou.com yum.repos.d]# yum install nginxLoaded plugins: fastes ...
- mysql (主从复制)(proxy , Amoeba)
原址如下: http://heylinux.com/archives/1004.html Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中 ...