gpt4 book ai didi

objective-c - 类别中重写的方法是否始终优先于原始实现?

转载 作者:搜寻专家 更新时间:2023-10-30 20:03:01 25 4
gpt4 key购买 nike

<分区>

我知道不鼓励使用类别覆盖方法。尽管如此,我必须处理一些执行此操作的代码。当我运行以下代码时,我最初感到惊讶的是我的类别方法在这两种情况下都被调用了,尽管考虑到链接器必须如何在链接时为任何给定符号做出决定让我感觉更好。

我的问题:假设除了我创建的类别之外没有其他类别在起作用,我是否保证我的类别中的实现将始终是调用的逻辑,只要它的标题是从某个地方导入的?

someObject.h

#import <Foundation/Foundation.h>

@interface SomeObject : NSObject

- (void)doSomething;

@end

someObject.m

#import "SomeObject.h"

@implementation SomeObject

- (void)doSomething
{
NSLog(@"Original");
}

@end

someObject+Cat.h

#import <Foundation/Foundation.h>

#import "SomeObject.h"

@interface SomeObject (SomeObject)

- (void)doSomething;

@end

someObject+Cat.m

#import "SomeObject+Cat.h"

@implementation SomeObject (SomeObject)

- (void)doSomething
{
NSLog(@"New!");
}

@end

someObjectUser.h

#import <Foundation/Foundation.h>

@interface SomeObjectUser : NSObject

- (void)useSomeObject;

@end

someObjectUser.m

#import "SomeObjectUser.h"

#import "SomeObject.h"

@implementation SomeObjectUser

- (void)useSomeObject
{
[[SomeObject new] doSomething];
}

@end

测试.m

- (void)testExample
{
[[SomeObject new] doSomething];
[[SomeObjectUser new] useSomeObject];
}

结果

2013-02-28 11:32:37.417 CategoryExample[933:907] New!
2013-02-28 11:32:37.419 CategoryExample[933:907] New!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com