Objective C中的构造函数

Ang*_*gus 30 objective-c

我是iPhone开发的初学者.我正在尝试下面的示例程序.

我没有在类B.中调用+(void)初始化和 - (id)init方法.但是它会被自动调用.

- (void)intialise是否等于Objective C中的默认构造函数.

[super init]是否指向NSObject.

如果我没有使用 - (id)init方法,我会收到一个警告,表明该类的实现不完整.

ClassA.h

#import <Foundation/Foundation.h>

static int ab;

@interface ClassA : NSObject {
    int a;
}

+ (void) initialize;
- (id) init;
- (void) displayNumOfInstance;
- (void) disp;

@end
Run Code Online (Sandbox Code Playgroud)

ClassA.m

#import "ClassA.h"

@implementation ClassA

+ (void) initialize
{
    ab=0;
}

- (id) init
{
    self = [super init];
    if (self!=nil) {
        ab++;
    }
    return self;
}

- (void) displayNumOfInstance
{
    NSLog(@"Number of instances of this class:%d",ab);
}

- (void) disp
{
    NSLog(@"The value is %d",ab);
}

@end
Run Code Online (Sandbox Code Playgroud)

ClassB.h

#import <Foundation/Foundation.h>
#import "ClassA.h"

@interface ClassB : ClassA {

}

- (void) display;

@end
Run Code Online (Sandbox Code Playgroud)

ClassB.m

#import "ClassB.h"

@implementation ClassB

- (void) display
{
    ab=20;
    NSLog(@"The value ab is %d",ab);
}

@end
Run Code Online (Sandbox Code Playgroud)

class2.m

#import <Foundation/Foundation.h>
#import "ClassA.h"

int main (int argc, const char * argv[]) {
    ClassA *a = [[ClassA alloc]init];
    [a disp];
    [a release];

    ClassB *b = [[ClassB alloc]init];
    [b display];
    [b release];

    ClassA *a1 = [[ClassA alloc]init];
    [a1 disp];
    [a1 release];

    ClassB *b1 = [[ClassB alloc]init];
    [b1 display];
    [b1 release];

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

2011-04-30 15:31:42.490 class2[1674:a0f] 1
2011-04-30 15:31:42.493 class2[1674:a0f] The value ab is 20
2011-04-30 15:31:42.494 class2[1674:a0f] 2
2011-04-30 15:31:42.495 class2[1674:a0f] The value ab is 20
Run Code Online (Sandbox Code Playgroud)

Wol*_*urs 58

默认构造通常以具有以下格式-init或此类型的任何变体开头,例如-initWithFrame:.

该方法+initialize是一个类方法(静态方法),在应用程序启动时至少调用一次.您可以使用此方法初始化在类的所有实例中都有用的静态变量.此方法可能对例如初始化类的共享缓存或共享查找映射很有用.

因为NSObject-init方法是指定的初始化程序,但对于其他类,这可能不同.Apple使用NS_DESIGNATED_INITIALIZER宏在其类头中记录指定的初始化程序.例如,UIView子类应该覆盖-initWithFrame:,-initWithCoder:而是将这些方法标记为指定的初始化程序.

在子类化和实现自定义指定的初始化程序时,不要忘记初始化超类.例如,让我们有一个UIView具有自定义指定初始值设定项的子类-initWithFrame:title:.我们将按如下方式实施:

// A custom designated initializer for an UIView subclass. 
- (id)initWithFrame:(CGRect)frame title:(NSString *)title 
{
    // Initialize the superclass first. 
    //
    // Make sure initialization was successful by making sure 
    // an instance was returned. If initialization fails, e.g. 
    // because we run out of memory, the returned value would
    // be nil.
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Superclass successfully initialized.
        self.titleLabel.text = title
    }
    return self;
}

// Override the designated initializer from the superclass to 
// make sure the new designated initializer from this class is 
// used instead.
- (id)initWithFrame:(CGRect)frame
{
    return [[self alloc] initWithFrame:frame title:@"Untitled"];
}
Run Code Online (Sandbox Code Playgroud)

有关初始化的更多详细信息,请访问Apple Developer网站: