一、声明类接口步骤:

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的更多相关文章

  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. windows 下的一些常用命令提示符

    windows下dos命令窗口输入 netstat -ano即可查看端口使用情况, 如果要查看指定端口是否被占用 使用命令netstat -ano|findstr 端口号, 例如要查看8080端口号是 ...

  2. Codefroces 628B New Skateboard(数位+思维)

    题目链接:http://codeforces.com/contest/628/problem/B 题目大意:给你一段数字串s(1?≤?|s|?≤?3·10^5),求该字符串有多少子串是4的倍数.解题思 ...

  3. kickstart配置LINUX无人值守选项--rootpw

    linux kickstart rootpw密码可以使用明文,也可以使用加密过的值(密码为:IPPBXADMINROOT) 注意:在这里要使用加密过的值,否则安全性就太低了 rootpw --iscr ...

  4. django model常用字段类型

    摘自 http://www.cnblogs.com/wt869054461/p/4014271.html V=models.AutoField(**options) #int:在Django代码内是自 ...

  5. Linux下的堆off-by-one的利用

    这篇稿子已经投到了360安全播报,http://bobao.360.cn/learning/detail/3113.html

  6. 使用mybatis-generator-core自动生成代码

    SSM框架可以使用mybatis-generator-core-1.3.2.jar来自动生成代码,以下是配置和使用的代码. generatorConfig.xml <?xml version=& ...

  7. 安装VM虚拟机提示 尝试创建目录 C:\Public\documents\SharedVirtual Machines 时发生错误解决方法

    把Windows Defender安全中心的“受控制文件夹的访问”给关闭了,然后就可以顺利安装上了. 作者:耑新新,发布于  博客园 转载请注明出处,欢迎邮件交流:zhuanxinxin@foxmai ...

  8. .NETCore分布式微服务站点设计(1)-概念图

    自己画了一个简略结构图,准备按照这个搭建一套微服务型的站点 利用Identityserver4+Redis+Sqlserver+Swagger+阿里云OSS+RabbitMQ+Nginx来实现,按照自 ...

  9. Mendeley文献管理软件使用介绍

    <!DOCTYPE html> New Document /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) / / ...

  10. JAVAEE——SpringMVC第二天:高级参数绑定、@RequestMapping、方法返回值、异常处理、图片上传、Json交互、实现RESTful、拦截器

    1. 课前回顾 https://www.cnblogs.com/xieyupeng/p/9093661.html 2. 课程计划 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 ...