【OC语法快览】三、创建实例对象

本文深入探讨了Objective-C中创建对象的两种主要方法:自动方式和人工方式,详细解释了每种方式的工作原理,并通过示例代码加以说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Creating Objects

       创建对象


There are two main ways to create an object. The first is the one you saw before:
创建对象主要有两种方法。第一种如下:
 
NSString* myString = [NSString string];
This is the more convenient automatic style. In this case, you are creating an autoreleased object, which we'll look at in more detail later. In many cases, though, you need to create an object using themanual style:
上面这种是比较方便自动的方式。这种情况下,创建了一个自动释放的对象,接下来我们会探究其更多细节。在更多的情况下,你需要使用人工方式来创建对象:
 
NSString* myString = [[NSStringalloc]init];
This is a nested method call. The first is the alloc method called on NSString itself. This is a relatively low-level call which reserves memory and instantiates an object. 
这是一种嵌套方法调用。首先是NSString调用alloc方法。这是一种相对底层的方法调用,用来获得内存和初始实例化对象。

T he second piece is a call to init on the new object. The init implementation usually does basic setup, such as creating instance variables. The details of that are unknown to you as a client of the class. 
第二个调用的是新实例对象的init方法。Init方法通常实现一些基本的启动动作,比如创建实例变量。Init方法的细节对调用者是透明的。

In some cases, you may use a different version of init which takes input:
在某些情况下,你可能使用新版本的init方法——带有输入参数。
 
NSNumber* value = [[NSNumber alloc] initWithFloat:1.0];
原文: learn_objective_C  part 3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值