前言
类 是具有相同特征和行为的事物的抽象
万事万物皆对象
- 对象是类的实例
- 类是对象的类型
面向对象的特点分为:
- 封装
- 继承
- 多态
如何看懂OC中的一个类
在OC中
一个类中的成员, 分为实例变量和方法两种
写在.h文件中的方法在类外是可以调用的.
而实例变量虽然写在.h文件中, 但是它们能否在类外可以调用需要根据他们的修饰符定义的:
实例变量的可见度
可见度 | 特点 |
---|---|
@public | 实例变量可以在类的外部和内部操作 |
@private | 实例变量只能在该类内访问 |
@protected(默认的) | 实例变量只能在该类和其子类内操作 |
@private 和@public 在实际开发中基本不使用
- 对于private和protected修饰的实例变量, 不能在类外进行调用 而public可以在类外对实例变量进行赋值, 但是这样破坏了面向对象的封装性.
如果我们需要对实例变量进行取值, 赋值操作. 可以在类内定义方法. 通过调用方法来获得实例变量的值.
注意: 类方法不能直接调用实例变量. 因为调用类方法的时候, 没有创建对象, 这时候没有在堆中开辟空间, 也没有进行赋值. 也就是说实例变量此时在内存中并不存在, 所以调用不了.
自定义初始化方法可以对实例变量进行赋值, 但是不能简单有效的对某个成员变量进行赋值.
要方便有效准确的对某一个实例变量进行取值, 赋值的操作, 通过以下方法:
-
先定义一个类:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Person</span> : <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NSObject</span></span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@protected</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 声明实例变量都是protected修饰</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_name; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 姓名</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_gender; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 性别</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_age; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 年龄</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
-
定义一个对象方法, 取出指定的实例变量的值(在类中封装一个取值方法)
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSString</span> *)getName; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 在.h文件中声明 </span>-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSString</span> *)getName { <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 在.m文件中实现 </span> return _name; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
-
定义一个对象方法, 更改指定的实例变量的值(在类中封装一个赋值方法)
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-<span class="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">setName:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSString</span> *)name; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 在.h文件中声明 </span>-<span class="ruby" style="box-sizing: border-box;"> (void)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">setName:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSString</span> *)name { <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 在.m文件中实现 </span> _name = name; } </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
getName, setName方法称为实例变量name的setter getter方法
既然有专门的名称, 自然有专门的命名规范
setter方法: set+实例变量的名字(忽略下划线 首字母大写) 参数名=实例变量名(忽略下划线)
如:
- (void)setName:(NSString *)name;
getter方法:方法名就等于实例变量名(忽略下划线)
如:
- (NSString *)name;
关于方法名
例如自定义方法:
<code class="hljs erlang has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-pp" style="box-sizing: border-box;"><span style="font-size:18px;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(instancetype)</span>initWithName:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSString</span> *)</span>name age:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSString</span> *)</span>age sex:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSString</span> *)</span>sex;</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li></ul>
它的方法名是:
initWithName:age:sex:
值得注意的是, 冒号也是方法名的一部分, 不可缺少, 冒号是标识有参数.
setter getter方法名:
如以上定义的setter getter方法, 它们的名字分别是:
setName:
和 name
同时修改两个成员变量的方法:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name gender:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)gender; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在.h文件中声明</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name gender:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)gender { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在.m文件中实现</span> _name = name; _gender = gender; }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li></ul>
p.s. OC是根据:来识别参数的个数的
关于创建对象
Person *p = [[Person alloc] init];
使用NSLog(@"%@", p);
打印p的信息
通过占位符%@打印对象p的信息, 是调用了继承自父类的方法(继承随后的文章会介绍)
- (NSString *)description;
可以通过重写该方法, 使程序调用
NSLog(@"%@", p);
的时候打印出想要显示的结果
如想要根据自己的意愿打印Person类对象的所有信息, 可以重写description方法如下:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">- (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)description { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"姓名:%@,性别:%@,年龄:%@"</span>,_name, _gender, _age]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> str; }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li></ul>
附:stringWithFormat: 是格式化拼接字符串方法,是NSString类的类方法. 是OC中非常常用的方法
互相引用会导致错误
定义两个类 Man 和Woman
如在Man类中
#import "Woman.h"
在Woman类中
#import "Man.h"
这样互相引用会导致循环引用, 因而报错.
原理是:
在头文件A中import 头文件B, 而头文件B中又import了头文件A,这会导致A运行到import语句, 跳转到了头文件B, 头文件B运行到improt语句又跳回了头文件A, 而头文件A再次运行到import语句又跳到了B… 像这样导致编译器无限的在两个头文件中跳转来跳转去, 导致死循环, 直到崩溃.
解决方法是:
在其中一个类中用@class 类名;的方法来解决
如在Man类中
#import "Woman.h"
在Woman类中
@class Man;
此句的意思是声明Man是一个类 如果需要在.m文件中用到Man类中的方法, 还需要在#import “Man.h”(在.m文件中声明)
此时在Man中可以定义一个Woman类的实例变量
在Woman中也可以定义一个Man类的实例变量
在出现循环导入的时候, 注意初始化的时候会出现赋的值未初始化而无法进行赋值的情况.
引用语句中
使用#include引头文件的时候不能重复导入
#import可以重复导入 , 但是不能循环导入
重复导入是在同一个文件中 多次使用import “XXX.h” 语句调用同一个头文件.
循环导入是在A文件中import B文件的头文件, 而B文件的头文件中又import了A文件的头文件, 导致编译器无限的在两个头文件中跳转来跳转去, 导致死循环, 直到崩溃.
例题如下:
创建男人类:
属性有: 姓名, 工作, 钱, 妻子
方法有: 看篮球, 赚钱
女人类: 姓名, 颜值, 丈夫, 孩子
方法有: 购物
宝宝类: 姓名, 性别
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Man.h</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Woman.h"</span></span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Man</span> : <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NSObject</span></span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_name; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 姓名</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_job; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 工作</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_money; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 钱</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 复合: 在本类中 声明了一个其他类的对象作为本类的实例变量</span> Woman *_wife; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 妻子</span> } - (instancetype)initWithName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name job:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)job money:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)money; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setJob:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)job; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)job; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setMoney:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)money; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)money; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setWife:(Woman *)wife; - (Woman *)wife; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)watchBasketBall; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)makeMoney; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)description; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Man.m</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Man.h"</span></span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Man</span></span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 可能在初始化Man的对象的时候, Woman对象尚未创建, 所以先不给Man里的实例变量Woman进行赋值</span> - (instancetype)initWithName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name job:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)job money:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)money { _name = name; _job = job; _money = money; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name { _name = name; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _name; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setJob:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)job { _job = job; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)job { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _job; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setMoney:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)money { _money = money; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)money { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _money; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setWife:(Woman *)wife { _wife = wife; } - (Woman *)wife { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _wife; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)watchBasketBall { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"看篮球"</span>); } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)makeMoney { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"挣钱"</span>); } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)description { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 输出妻子的姓名, 而不是妻子的全部信息. 请注意</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"姓名:%@,工作:%@,钱:%@,妻子:%@"</span>, _name, _job, _money, [_wife name]]; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Woman.h</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Baby.h"</span></span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Man</span>; // 用@<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">class</span> 关键字声明<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Man</span>是一个类 由于<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Man</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">h</span>已经<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">import</span> “<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Woman</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">h</span>”, 所以不能再在<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Woman</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">h</span>中<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">import</span> ”<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Man</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">h</span>"</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Woman</span> : <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NSObject</span></span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_name; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 姓名</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_beautifulValue; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 颜值</span> Man *_husband; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 丈夫 在这里将Man仅仅当成一个类名来使用, 在这里虽然husbend声明的是Man的对象 但它没有保存Man类中的实例变量和方法, 若要调用到Man类里的方法, 只能通过import ”Man.h”的形式</span> Baby *_baby; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 孩子</span> } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 由于Woman对象初始化的时候 Man对象和Baby对象可能还未创建, 无法给Woman对象中的实例变量_husband, _baby赋值, 所以先不在初始化中对这两个变量进行初始化.</span> - (instancetype)initWithName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name beautifulValue:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)beautifulValue; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setBeautifulValue:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)beautifulValue; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)beautifulValue; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setHusBand:(Man *)husband; - (Man *)husband; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setBaby:(Baby *)baby; - (Baby *)baby; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)bayBayBay; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)description; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Woman.m</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Woman.h"</span></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import “Man.h” // 在这里import “Man.h” 可以通过.h中声明的Man变量 调用Man类里的方法.</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Woman</span></span> - (instancetype)initWithName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name beautifulValue:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)beautifulValue { _name = name; _beautifulValue = beautifulValue; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name { _name = name; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _name; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setBeautifulValue:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)beautifulValue { _beautifulValue = beautifulValue; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)beautifulValue { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _beautifulValue; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setHusBand:(Man *)husband { _husband = husband; } - (Man *)husband { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _husband; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setBaby:(Baby *)baby { _baby = baby; } - (Baby *)baby { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _baby; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)bayBayBay { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"买东西"</span>); } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)description { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"姓名:%@,颜值:%@,丈夫:%@,孩子:%@"</span>,_name, _beautifulValue, [_husband name], [_baby name]]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在这里显示的是男人的名字和孩子的名字 为的是避免出现循环调用 导致死循环, 下文会进行详细解释</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Baby.h</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Baby</span> : <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NSObject</span></span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_name; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 姓名</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *_sex; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 性别</span> } - (instancetype)initWihtName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name sex:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)sex; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setSex:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)sex; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)sex; - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)description; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Baby.m</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Baby.h"</span></span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Baby</span></span> - (instancetype)initWihtName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name sex:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)sex { _name = name; _sex = sex; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name { _name = name; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _name; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setSex:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)sex { _sex = sex; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)sex { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _sex; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)description { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"姓名:%@,性别:%@"</span>,_name,_sex]; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// main.m</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;"><Foundation/Foundation.h></span></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Man.h"</span></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Woman.h"</span></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"Baby.h"</span></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[]) { Man *man = [[Man alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"l"</span> job:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ios"</span> money:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"12k/m"</span>]; Woman *women = [[Woman alloc] initWithName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"y"</span> beautifulValue:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"10"</span>]; Baby *baby = [[Baby alloc] initWihtName:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ly"</span> sex:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"女"</span>]; [women setBaby:baby]; [women setHusBend:man]; [man setWife:women]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, man); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">13</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">14</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">15</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">16</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">17</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">18</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">19</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">20</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">21</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">22</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">23</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">24</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">25</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">26</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">27</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">28</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">29</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">30</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">31</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">32</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">33</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">34</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">35</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">36</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">37</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">38</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">39</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">40</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">41</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">42</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">43</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">44</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">45</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">46</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">47</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">48</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">49</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">50</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">51</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">52</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">53</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">54</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">55</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">56</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">57</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">58</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">59</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">60</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">61</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">62</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">63</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">64</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">65</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">66</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">67</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">68</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">69</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">70</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">71</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">72</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">73</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">74</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">75</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">76</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">77</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">78</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">79</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">80</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">81</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">82</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">83</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">84</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">85</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">86</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">87</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">88</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">89</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">90</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">91</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">92</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">93</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">94</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">95</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">96</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">97</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">98</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">99</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">100</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">101</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">102</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">103</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">104</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">105</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">106</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">107</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">108</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">109</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">110</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">111</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">112</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">113</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">114</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">115</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">116</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">117</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">118</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">119</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">120</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">121</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">122</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">123</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">124</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">125</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">126</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">127</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">128</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">129</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">130</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">131</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">132</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">133</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">134</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">135</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">136</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">137</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">138</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">139</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">140</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">141</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">142</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">143</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">144</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">145</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">146</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">147</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">148</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">149</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">150</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">151</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">152</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">153</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">154</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">155</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">156</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">157</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">158</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">159</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">160</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">161</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">162</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">163</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">164</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">165</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">166</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">167</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">168</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">169</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">170</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">171</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">172</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">173</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">174</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">175</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">176</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">177</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">178</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">179</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">180</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">181</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">182</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">183</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">184</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">185</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">186</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">187</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">188</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">189</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">190</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">191</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">192</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">193</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">194</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">195</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">196</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">197</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">198</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">199</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">200</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">201</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">202</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">203</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">204</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">205</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">206</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">207</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">208</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">209</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">210</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">211</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">212</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">213</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">214</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">215</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">216</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">217</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">218</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">219</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">220</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">221</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">222</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">223</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">224</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">225</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">226</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">227</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">228</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">229</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">230</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">231</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">232</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">233</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">234</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">235</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">236</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">237</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">238</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">239</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">240</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">241</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">242</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">243</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">244</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">245</span></li></ul>
要点1:
- 在Woman.h中用
@class Man;
而不是import "Man.h"
.是为了避免循环引用.
要点2:
- Woman类中的description调用了Man对象,
NSLog(@"%@", man);
输出的是man对象, 而输出一个对象的内容是这个对象的description方法, 而若此时man的description又出现NSLog(@"%@", woman);
又调用了woman的description方法, 这样就发现两者无限循环调用, 直到内存满了, 程序崩溃. 所以两个对象不能同时输出对方的对象. 只有一方输出另一方的对象是可以接受的, 两者同时输出另一方, 是会崩溃的. 因而程序中, 输出的不是对象本身, 而是对象的实例变量, 这样就可以绕过循环调用.