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

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语法快览】六、类实现

时间: 2024-08-06 07:57:36

【OC语法快览】六、类实现的相关文章

【OC语法快览】五、设计类接口

Designing a Class Interface     设计类接口 The Objective-C syntax for creating a class is very simple. It typically comes in two parts. 创建类的语法是很简单的,通常包括两部分. The class interface is usually stored in the ClassName.h file, and defines instance variables and

【OC语法快览】二、存取方法

Accessors 存取方法 All instance variables are private in Objective-C by default, so you should use accessors to get and set values in most cases. There are two syntaxes. This is the traditional 1.x syntax: OC中所有的实例变量默认是私有的,所以多数情况下你应该使用访问器来获得和设置实例变量的值.访问器

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

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 autorel

【OC语法快览】四、基础内存管理

Basic Memory Management                                                           基础内存管理 If you're writing an application for Mac OS X, you have the option to enable garbage collection. In general, this means that you don't have to think about memory

【OC语法快览】一、方法调用

调用方法 [object method]; [object methodWithInput:input]; output = [object methodWithOutput]; output = [object methodWithInputAndOutput:input]; id myObject = [NSString string]; NSString* myString = [NSString string]; 嵌套消息 function1 ( function2() ); [NSSt

(转载)OC语法总结

1.定义类:@interface 类名 : 父类@end 2.使用:(冒号)表示继承一个类Student : NSObject 3.使用()定义一个Catagory(类别) * 作用:在不改变原有类结构的基础上,扩展原有类的方法(不能扩展属性),但不建议重载原有类的方法 * 开发工具默认生成的文件为:类名+Catagory名称 * Catagory可以写在单独的文件中,也可以写在原有类的文件中,如何写根据需求来决定. 4.使用<>表示实现一个Protocol(协议),如需实现多个协议,将协议名

【IOS】IOS快速入门之OC语法

Objective-C 是 C 语言的超集 您还可以访问标准 C 库例程,例如在 stdlib.h 和 stdio.h 中声明的那些例程. Objective-C 还是一种非常动态的程序设计语言,而且这种动态是其最大优势.这种动态体现在它允许在运行应用程序时(即运行时)才去确定其行为,而不是在生成期间就已固定下来.因此,Objective-C 的动态机制让程序免受约束(编译和链接程序时施加的约束):进而在用户控制下,将大多数符号解析责任转移到运行时. 当您想要在源代码中包括头文件时,请在头文件或

转:OC语法总结

1.定义类:@interface 类名 : 父类@end 2.使用:(冒号)表示继承一个类Student : NSObject 3.使用()定义一个Catagory(类别) * 作用:在不改变原有类结构的基础上,扩展原有类的方法(不能扩展属性),但不建议重载原有类的方法 * 开发工具默认生成的文件为:类名+Catagory名称 * Catagory可以写在单独的文件中,也可以写在原有类的文件中,如何写根据需求来决定. 4.使用<>表示实现一个Protocol(协议),如需实现多个协议,将协议名

OC语法——Object-C retain、copy、mutableCopy的详细分析

OC语法中的retain.copy.mutableCopy 大家都基本知道它的基本意思,但是对于mutable类型和immutable类型的处理上有很多童鞋并没有真正测试过,今天就和大家分享下: 1.先来看下immutable类型的非容器类: NSString的retain.copy和mutableCopy的测试 NSString *string = @"abc"; NSString *retainString = [string retain]; NSString *copyStri