【OC语法快览】六、类实现

简介: Class Implementation      类实现 Let's create an implementation, starting with the getters: 接下来创建一个类实现,从访问器开始: #import "Photo.

Class Implementation

     类实现

Let's create an implementation, starting with the getters:
接下来创建一个类实现,从访问器开始:

#import "Photo.h"

@implementation Photo
    
- (NSString*) caption {
    return caption;
}

- (NSString*) photographer {
    return photographer;
}

@end


This part of the code starts with  @implementation and the class name, and has  @end, just like the interface. All methods must appear between these two statements. 
跟类接口一样,这段代码都有@end结束,不同的是以   @implementation加上类名开始。所有的方法必须出现在这两句中间。

The getters should look very familiar if you've ever written code, so let's move on to the setters, which need a bit more explanation:
如果你曾经写过代码,你会很熟悉访问器,所以把目光转移到设值方法上去,这里需要多解释一下: 

 
- (void) setCaption: (NSString*)input
{
    [caption autorelease];
    caption = [input retain];
}

- (void) setPhotographer: (NSString*)input
{
    [photographer autorelease];
    photographer = [input retain];
}


Each setter deals with two variables. The first is a reference to the existing object, and the second is the new input object. In a garbage collected environment, we could just set the new value directly:
每个设值方法可以处理两种变量,第一种是引用已经存在的对象,第二章是新建一个输入对象。在开启GC功能的环境下,可以直接赋值:
  
      
 
- (void) setCaption: (NSString*)input {
    caption = input;
}    


      But if you can't use garbage collection, you need to  release  the old object, and  retain  the new one. 
      但是,如果你不能使用GC功能,你需要释放release老对象和获取保留retain新的对象。

There are actually two ways to free a reference to an object:  release and  autorelease. The standard release will remove the reference immediately. The autorelease method will release it sometime soon, but it will definitely stay around until the end of the current function (unless you add custom code to specifically change this). 
实际上有两种方式释放对一个对象的引用: release 和 autorelease。前者是标准的释放,立即移除引用。后者自动释放autorelease则会在不远的将来尽快释放掉,最久也会在方法结束之前释放掉,除非你做了特别处理。

The autorelease method is safer inside a setter because the variables for the new and old values could point to the same object. You wouldn't want to immediately release an object which you're about to retain. 
自动释放autorelease在设值方法中是相对安全的,因为新老变量值可以指向同一个对象。对于你想要保留retain的对象,你不能立即释放它。

This may seem confusing right now, but it will make more sense as you progress. You don't need to understand it all yet.
现在可能有些疑惑了,但会让你进步。你现在不需要完全理解它。
 

Init

       初始化方法

We can create an init method to set inital values for our instance variables:
我们可以新建一个初始化方法,给实例变量赋初值。

- (id) init
{
    if ( self = [super init] )
    {
        [self setCaption:@"Default Caption"];
        [self setPhotographer:@"Default Photographer"];
    }
    return self;
}



This is fairly self-explanatory, though the second line may look a bit unusual. This is a single equals sign, which assigns the result of  [super init] to  self
这是完全不言自明的,尽管第二行可能看起来有点不同寻常。这是一个单等号,把   [super init]  的结果赋值给self。

This essentially just asks the superclass to do its own initialization. The  if statement is verifying that the initialization was successful before trying to set default values.
      这句话本意是要求该类的父类实例化。if语句用来判断实例化是否成功,成功后再初始化变量。
 

Dealloc

       回收内存

The  dealloc method is called on an object when it is being removed from memory. This is usually the best time to release references to all of your child instance variables:
dealloc方法在这个对象从内存中移除的时候调用。这通常是释放对子实例变量的引用的最佳时机: 

 
- (void) dealloc
{
    [caption release];
    [photographer release];
    [super dealloc];
}



On the first two lines, we just send release to each of the instance variables. We don't need to use autorelease here, and the standard release is a bit faster. 
在前两行,我们只是分别给各个实例变量发送 release消息。我们不用autorelease,因为release更快。

The last line is very important. We have to send the message  [super dealloc] to ask the superclass to do its cleanup. If we don't do this, the object will not be removed, which is a memory leak. 
最后一行很重要。我们需要给父类发送 [super dealloc]  消息让它自我释放。如果没有这么做,对象不会被移除,从而导致内存泄露。

The dealloc method is not called on objects if garbage collection is enabled. Instead, you implement the  finalize method.
如果GC功能开启, dealloc方法将不被调用,这种情况下要实现finalize方法。


原文:
learn_objective_C part 6



相关文章
OC泛型的使用
在声明类的时候,不确定某种属性或方法类型,在使用这个类的时候才确定,就可以采用泛型 如果没有自定义泛型,默认就是id类型
328 0