ios - 调用父类(super class)指定的初始化程序调用子类

标签 ios objective-c designated-initializer

我遇到了一个看起来很简单的问题,但我只是不知道为什么它会这样工作。

我有一个 Shape 类,它有 Square 的子类。

当我调用 Square 并调用其指定的初始化程序时,在 self = [super init] 中它调用父类(super class)。然而,当父类(super class)调用其指定的初始化程序(与子类之一同名)时,它会调用子类。

我最终得到的是子类在父类(super class)上调用 init 并调用子类初始化程序的无限循环。

我该如何解决这个问题?我是否应该确保初始化程序的名称足够不同,以免发生这种情况?

Shape.h

#import <Foundation/Foundation.h>

@interface Shape : NSObject

@property (nonatomic) CGPoint position;
@property (nonatomic) float width;
@property (nonatomic) float height;
@property (nonatomic, readonly) float area;

- (id)initWithWidth:(float)width andHeight:(float)height andPosition:(CGPoint)position;
- (id)initWithWidth:(float)width andHeight:(float)height;
- (id)initWithWidth:(float)width;
- (id)init;

- (NSString *)drawShape;

@end

-

Shape.m

#import "Shape.h"

@implementation Shape

- (id)initWithWidth:(float)width andHeight:(float)height andPosition:(CGPoint)position
{
    self = [super init];

    if (self) {
        self.width = width;
        self.height = height;
        self.position = position;
    }

    return self;
}

- (id)initWithWidth:(float)width andHeight:(float)height
{
    return [self initWithWidth:width andHeight:height andPosition:CGPointMake(100, 100)];
}

- (id)initWithWidth:(float)width
{
    return [self initWithWidth:width andHeight:1.0f andPosition:CGPointMake(100, 100)];
}

- (id)init
{
    CGPoint defaultPoint = CGPointMake(100, 100);

    return [self initWithWidth:1.0 andHeight:1.0 andPosition:defaultPoint];
}

- (NSString *)drawShape
{
    NSString *outputShape = [NSString stringWithFormat:@"Drawing shape - (%.2f,%.2f), width - %f, height - %f", self.position.x, self.position.y, self.width, self.height];

    NSLog(@"%@", outputShape);

    return outputShape;
}

@end

-

Square.h

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

@interface Square : Shape

- (id)initWithWidth:(float)width andPosition:(CGPoint)position;
- (id)initWithWidth:(float)width andHeight:(float)height andPosition:(CGPoint)position;
- (id)initWithWidth:(float)width andHeight:(float)height;
- (id)initWithWidth:(float)width;

@end

-

Square.m

#import "Square.h"

@implementation Square

- (id) initWithWidth:(float)width andPosition:(CGPoint)position
{
    self = [super init];

    if (self) {
        self.width = width;
        self.height = width;
        self.position = position;
    }

    return self;
}

// Returning the width as the width and height as you can't make a square with different sides
- (id)initWithWidth:(float)width andHeight:(float)height andPosition:(CGPoint)position
{
    return [self initWithWidth:width andPosition:position];
}

- (id)initWithWidth:(float)width andHeight:(float)height
{
    return [self initWithWidth:width andPosition:CGPointMake(100, 100)];
}

- (id)initWithWidth:(float)width
{
    return [self initWithWidth:width andPosition:CGPointMake(100, 100)];
}

- (NSString *)drawShape
{
    NSString *outputShape = [NSString stringWithFormat:@"Drawing shape - (%.2f,%.2f), width - %.2f, height - %.2f", self.position.x, self.position.y, self.width, self.height];

    NSLog(@"%@", outputShape);

    return outputShape;
}

@end

最佳答案

这里要做的是重新实现-[Square initWithWidth:andPosition:],如下所示:

- (id)initWithWidth:(float)width andPosition:(CGPoint)position
{
    self = [super initWithWidth:width andHeight:width andPosition:position];
    return self;
}

关于ios - 调用父类(super class)指定的初始化程序调用子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884308/

相关文章:

ios - 为什么指定的初始化器没有被调用

ios - 在导航栏中显示大量信息

ios - 在 iOS 中添加了 Google Crashlytics - 未连接到 Firebase Crashlytics 仪表板

ios - missKeyboard - 无法识别的选择器

ios - 创建一个处理通用类型的函数来访问不同 View 的属性

iphone - 就iPhone应用程序而言,当后台应用程序恢复事件时自动重新加载显示页面的方式

C++20 指定具有模板类型的初始值设定项

ios - 指定初始化程序?

ios - 在iOS中的 slider 值上旋转图像

ios - 防止 UIWebView 为 tel 链接显示 UIPopoverController