Objective-C对象初始化 、 实例方法和参数 、 类方法 、 工厂方法 、 单例模式
1 重构Point2类
1.1 问题
本案例使用初始化方法重构Point2类,类中有横坐标x、纵坐标y两个属性,并且有一个能显示位置show方法。在主程序中创建两个Point2类的对象,设置其横纵坐标,并将它们显示出来。
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:定义类Point2
由于是对Point2类的重构,所以在Day02工程中新添加Point2.h文件用于定义新的类Point2。
代码如下所示:
- @interface Point2 : NSObject
- {
- }
- @end
步骤二:在Point2类中添加属性
在Point2类中添加两个属性,它们为横坐标x,为纵坐标y。
- #import <Foundation/Foundation.h>
- @interface Point2 : NSObject
- {
- }
- @property int x, y;
- @end
步骤三:在Point2类中添加初始化方法
首先,在类Point2的声明中添加无参初始化方法和有参初始化方法的声明。
代码如下所示:
- #import <Foundation/Foundation.h>
- @interface Point2 : NSObject
- {
- }
- @property int x, y;
- -(id)init;
- -(id)initWithX:(int)x andY:(int)y;
- @end
上述代码中,以下代码:
- -(id)init;
是无参初始化方法,该方法返回一个id类型的变量。id类型是Objective-C中的一种万能指针,相当于C语言中的void*这种数据类型。
上述代码中,以下代码:
- -(id)initWithX:(int)x andY:(int)y;
是有参初始化方法,该方法返回一个id类型的变量,同时具有两个形参,一个是整型变量x,另一个是整型变量y。
然后,在类Point2的实现部分,即在Point2.m文件中,添加无参初始化方法和有参初始化方法的实现。
代码如下所示:
- #import "Point2.h"
- @implementation Point2
- -(id)init
- {
- if (self = [super init])
- {
- _x = 10;
- _y = 20;
- }
- return self;
- }
- -(id)initWithX:(int)x andY:(int)y
- {
- if (self = [super init])
- {
- _x = x;
- _y = y;
- }
- return self;
- }
- @end
上述代码中,以下代码:
- if (self = [super init])
是首先发消息[super init]调用基类中的init方法获得self值。
步骤四:在Point2类中添加show方法
show方法用于在控制台上显示Point2类中的两个实例变量的值。
首先,在类Point2的声明中添加show方法的声明。
代码如下所示:
- #import <Foundation/Foundation.h>
- @interface Point2 : NSObject
- {
- }
- @property int x, y;
- -(id)init;
- -(id)initWithX:(int)x andY:(int)y;
- -(void)show;
- @end
然后,在类Point2的实现部分,即在Point2.m文件中,添加show方法的实现。
代码如下所示:
- #import "Point2.h"
- @implementation Point2
- -(id)init
- {
- if (self = [super init])
- {
- _x = 10;
- _y = 20;
- }
- return self;
- }
- -(id)initWithX:(int)x andY:(int)y
- {
- if (self = [super init])
- {
- _x = x;
- _y = y;
- }
- return self;
- }
- -(void)show
- {
- NSLog(@"x=%d,y=%d", _x, _y);
- }
- @end
步骤五:在主程序中使用Point2类
首先在Day02工程中新添加Point2Main.m文件,用于存储主程序,在主程序中定义两个Point2类的对象,同时使用无参初始化方法和有参初始化方法对两个对象的横坐标和纵坐标赋初值,最后打印这两个对象。
- #import "Point2.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- Point2 *p = [[Point2 alloc] init];
- [p show];
- Point2 *p1 = [[Point2 alloc] initWithX:55 andY:77];
- [p1 show];
- }
- return 0;
- }
1.3 完整代码
本案例中,类Point2声明,即Point2.h文件,完整代码如下所示:
- #import <Foundation/Foundation.h>
- @interface Point2 : NSObject
- {
- }
- @property int x, y;
- -(id)init;
- -(id)initWithX:(int)x andY:(int)y;
- -(void)show;
- @end
类Point2实现,即Point2.m文件,完整代码如下所示:
- #import "Point2.h"
- @implementation Point2
- -(id)init
- {
- if (self = [super init])
- {
- _x = 10;
- _y = 20;
- }
- return self;
- }
- -(id)initWithX:(int)x andY:(int)y
- {
- if (self = [super init])
- {
- _x = x;
- _y = y;
- }
- return self;
- }
- -(void)show
- {
- NSLog(@"x=%d,y=%d", _x, _y);
- }
- @end
主程序,即Point2Main.m,完整代码如下所示:
- #import "Point2.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- Point2 *p = [[Point2 alloc] init];
- [p show];
- Point2 *p1 = [[Point2 alloc] initWithX:55 andY:77];
- [p1 show];
- }
- return 0;
- }
2 重构Point2类
2.1 问题
本案例使用工厂方法重构Point2类,类中有横坐标x、纵坐标y两个属性,并且有一个能显示位置show方法。在主程序中创建两个Point2类的对象,设置其横纵坐标,并将它们显示出来。
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:定义类Point2
由于是对Point2类的重构,所以在Day02工程中新添加Point3.h文件用于定义新的类Point2。
代码如下所示:
- @interface Point2 : NSObject
- {
- }
- @end
步骤二:在Point2类中添加属性、初始化方法和show方法
首先,在类Point2的声明中添加两个属性,同时再添加无参初始化方法和有参初始化方法的声明和添加show方法的声明。
代码如下所示:
- #import <Foundation/Foundation.h>
- @interface Point2 : NSObject
- {
- }
- @property int x, y;
- -(id)init;
- -(id)initWithX:(int)x andY:(int)y;
- -(void)show;
- @end
然后,在类Point2的实现部分,即在Point3.m文件中,添加无参初始化方法、有参初始化方法和show方法的实现。
- #import "Point3.h"
- @implementation Point2
- -(id)init
- {
- if (self = [super init])
- {
- _x = 10;
- _y = 20;
- }
- return self;
- }
- -(id)initWithX:(int)x andY:(int)y
- {
- if (self = [super init])
- {
- _x = x;
- _y = y;
- }
- return self;
- }
- -(void)show
- {
- NSLog(@"x=%d,y=%d", _x, _y);
- }
- @end
步骤三:在Point2类中添加工厂方法的声明
工厂方法本质上是类的静态方法,其调用是通过类名直接调用的,与实例方法不同,实例方法是通过对象来调用的。
首先,在类Point2的声明中添加无参工厂方法和有参工厂方法的声明。
代码如下所示:
- #import <Foundation/Foundation.h>
- @interface Point2 : NSObject
- {
- }
- @property int x, y;
- -(id)init;
- -(id)initWithX:(int)x andY:(int)y;
- +(id)created;
- +(id)createdWithX:(int)x andY:(int)y;
- -(void)show;
- @end
上述代码中,以下代码:
- +(id)created;
- +(id)createdWithX:(int)x andY:(int)y;
用于对工厂方法的声明,需要注意的是工厂方法以加号“+”开头,而实例方法则以减号“-”开头。
然后,在类Point2的实现部分,即在Point3.m文件中,添加无参工厂方法和有参工厂方法的实现。方法的函数体实际上就是创建一个对象,然后将其返回。
代码如下所示:
- #import "Point3.h"
- @implementation Point2
- -(id)init
- {
- if (self = [super init])
- {
- _x = 10;
- _y = 20;
- }
- return self;
- }
- -(id)initWithX:(int)x andY:(int)y
- {
- if (self = [super init])
- {
- _x = x;
- _y = y;
- }
- return self;
- }
- +(id)created
- {
- Point2 *p = [[Point2 alloc] init];
- return p;
- }
- +(id)createdWithX:(int)x andY:(int)y
- {
- Point2 *p = [[Point2 alloc] initWithX:x andY:y];
- return p;
- }
- -(void)show
- {
- NSLog(@"x=%d,y=%d", _x, _y);
- }
- @end
步骤四:在主程序中使用Point2类
首先在Day02工程中新添加Point3Main.m文件,用于存储主程序,在主程序中创建两个Point2类的对象,一个用无参工厂方法创建,另一个用有参工厂方法创建,最后打印这两个对象。
代码如下所示:
- #import "Point3.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- Point2 *p = [Point2 created];
- [p show];
- Point2 *p1 = [Point2 createdWithX:55 andY:77];
- [p1 show];
- }
- return 0;
- }
2.3 完整代码
本案例中,类Point2声明,即Point3.h文件,完整代码如下所示:
- #import <Foundation/Foundation.h>
- @interface Point2 : NSObject
- {
- }
- @property int x, y;
- -(id)init;
- -(id)initWithX:(int)x andY:(int)y;
- +(id)created;
- +(id)createdWithX:(int)x andY:(int)y;
- -(void)show;
- @end
类Point2实现,即Point3.m文件,完整代码如下所示:
- #import "Point3.h"
- @implementation Point2
- -(id)init
- {
- if (self = [super init])
- {
- _x = 10;
- _y = 20;
- }
- return self;
- }
- -(id)initWithX:(int)x andY:(int)y
- {
- if (self = [super init])
- {
- _x = x;
- _y = y;
- }
- return self;
- }
- +(id)created
- {
- Point2 *p = [[Point2 alloc] init];
- return p;
- }
- +(id)createdWithX:(int)x andY:(int)y
- {
- Point2 *p = [[Point2 alloc] initWithX:x andY:y];
- return p;
- }
- -(void)show
- {
- NSLog(@"x=%d,y=%d", _x, _y);
- }
- @end
主程序,即Point3Main.m,完整代码如下所示:
- #import "Point3.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // insert code here...
- Point2 *p = [Point2 created];
- [p show];
- Point2 *p1 = [Point2 createdWithX:55 andY:77];
- [p1 show];
- }
- return 0;
- }
Objective-C对象初始化 、 实例方法和参数 、 类方法 、 工厂方法 、 单例模式的更多相关文章
- Python32之类和对象2(self参数及魔法方法)
一.类方法中的self参数含义 在Python中类的方法都要有self参数,其实质为对类的实例化对象的绑定从而使得在类的实例化对象调用方法时能够确认出是对哪个对象进行操作. 带self的的参数是人家实 ...
- 对象初始化的完整过程(C#)
1.静态构造函数 在引入本文的主题之前,我们先来铺垫一下吧,看看静态构造函数的概念及用途. C#中允许创建无参数构造函数,该函数仅执行一次.它一般被用来初始化静态字段.CLR不能保证在某个特定时刻执行 ...
- python__高级 : 动态添加 对象属性, 类属性, 对象实例方法, 类静态方法, 类方法
给对象添加实例属性,可以直接这样 t.age = 18 ( 假设 t = Test() ) 给类添加类属性 , 也可以直接这样 Test.age = 18 那给对象添加实例方法,可以在类外面 ...
- Objective -C Object initialization 对象初始化
Objective -C Object initialization 对象初始化 1.1 Allocating Objects 分配对象 Allocation is the process by w ...
- C#语法糖之第二篇: 参数默认值和命名参数 对象初始化器与集合初始化器
今天继续写上一篇文章C#4.0语法糖之第二篇,在开始今天的文章之前感谢各位园友的支持,通过昨天写的文章,今天有很多园友们也提出了文章中的一些不足,再次感谢这些关心我的园友,在以后些文章的过程中不断的完 ...
- 第七节:语法总结(1)(自动属性、out参数、对象初始化器、var和dynamic等)
一. 语法糖简介 语法糖也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方 ...
- jQuery对象初始化的多种传参数形式
jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常 ...
- {{jQuery源码分析}}jQuery对象初始化的多种传参数形式
jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常 ...
- python-面向对象-09_类属性和类方法
类属性和类方法 目标 类的结构 类属性和实例属性 类方法和静态方法 01. 类的结构 1.1 术语 —— 实例 使用面相对象开发,第 1 步 是设计 类 使用 类名() 创建对象,创建对象 的动作有两 ...
随机推荐
- 推荐cms
推荐cms : 国外:drupal joomla wordpress 国内:phpcms
- C#基础学习文章导航
第一部分:入个门 C#入门篇-1:HelloWorld的类 C#入门篇-2:什么是变量 C#入门篇-3:数据类型及转换 C#入门篇-4:使用运算符 第二部分:流程控制语句 C#入门篇5-1:流程控制语 ...
- 一步一步配置NLB(续)之深入测试
在这里http://blog.csdn.net/haoxiaozigang1/article/details/12198679跟大家分享了NLB配置的过程,下面写一些对NLB不同情况的下测试的结果: ...
- js 数组去除空值
for(var i = 0 ;i<wordarr.length;i++) { if(wordarr[i] == "& ...
- HDU 1098 Ignatius's puzzle 费马小定理+扩展欧几里德算法
题目大意: 给定k,找到一个满足的a使任意的x都满足 f(x)=5*x^13+13*x^5+k*a*x 被65整除 推证: f(x) = (5*x^12 + 13 * x^4 + ak) * x 因为 ...
- 戴文的Linux内核专题:06配置内核(2)
转自Linux中国 这一部分我们讲配置内核IRQ子系统.中断请求(IRQ)是硬件发给处理器的一个信号,它暂时停止一个正在运行的程序并允许一个特殊的程序占用CPU运行. 这个目录中的第一个问题属于内核特 ...
- xlistview的XML(头)xlistview_header
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- InterruptedException 线程异常
InterruptedException 这个异常一般发生在线程中,当一个正在执行的线程被中断时就会出现这个异常-! 简单的说就是:假如有两个线程,第一个线程正在运行,第二个没有运行,这时第二个线程启 ...
- Queryable.GroupBy<TSource, TKey> 方法 (IQueryable<TSource>, Expression<Func<TSource, TKey>>) 转
根据指定的键选择器函数对序列中的元素进行分组. 命名空间: System.Linq程序集: System.Core(在 System.Core.dll 中) 语法 C# C++ F# VB p ...
- Allegro PCB SI (2)
整理一下在电研院学的si (虽然彩超的si在频率15Mhz以上后,si是失真的.昨晚遇到孔大哥也是这样说的,板级仿真,要layout过硬,然后找到合适的top test point) Allegro ...