为什么在调用自己的类方法时需要使用[self class] ?

时间:2021-10-27 23:47:29

I read a topic at here http://www.mikeash.com/pyblog/friday-qa-2010-05-14-what-every-apple-programmer-should-know.html. Mike said that "Always use [self class] when invoking your own class methods". But I don't understand why. Can you give an example ?

我在这里阅读了一个主题http://www.mikeash.com/pyblog/friday-qa-2010-05-14 what-every-apple program -should-know.html。Mike说“在调用自己的类方法时总是使用[self class]”。但我不明白为什么。你能举个例子吗?

2 个解决方案

#1


7  

Lets say that you have class foo, which have the following methods:

假设您有foo类,它有以下方法:

+(NSString*) bar{ return @"bar"; }

-(NSString*) barMethod{ return [[self class] bar]; }

Now lets say that you have a class foo2 which inherits from foo. If you override

现在假设有一个从foo继承的类foo2。如果你覆盖

+(NSString*) bar { return @"bar2" }

the method barMethod will return bar2, as you probably intended for it to.

barMethod将返回bar2,正如您可能希望的那样。

#2


6  

Unlike other OO languages, class methods in Objective-C are both inherited and can be overridden.

与其他OO语言不同,Objective-C中的类方法都是继承的,可以重写。

Thus, if you have:

因此,如果你有:

@immplementation Abstract // : NSObject
- (void) doSomethingClassy
{
    [Abstract classyThing];
}

+ (void) classyThing
{
    ... some classy code ...;
}
@end

@interface Concrete : Abstract
@end
@implementation Concrete
+ (void) classyThing
{
    ... some classy code ...;
    [super classThing];
}
@end

Then this won't call Concrete's +classyThing from Abstract's implementation of doSomethingClassy:

那么这就不会从抽象的doSomethingClassy实现中调用Concrete's +classyThing:

[[[Concrete alloc] init] doSomethingClassy];

Whereas if you modify doSomethingClassy to do [[self class] classyThing]; it'll work as expected.

然而,如果你修改doSomethingClassy来做[[self class] classyThing];它会正常工作。

(note that this is a concrete example of Liye Zhang's answer -- feel free to mark his correct as he was first, just not with quite as concrete of an example)

(请注意,这是张丽叶回答的一个具体例子——尽管他的回答是正确的,但他的回答并不像一个例子那么具体)

#1


7  

Lets say that you have class foo, which have the following methods:

假设您有foo类,它有以下方法:

+(NSString*) bar{ return @"bar"; }

-(NSString*) barMethod{ return [[self class] bar]; }

Now lets say that you have a class foo2 which inherits from foo. If you override

现在假设有一个从foo继承的类foo2。如果你覆盖

+(NSString*) bar { return @"bar2" }

the method barMethod will return bar2, as you probably intended for it to.

barMethod将返回bar2,正如您可能希望的那样。

#2


6  

Unlike other OO languages, class methods in Objective-C are both inherited and can be overridden.

与其他OO语言不同,Objective-C中的类方法都是继承的,可以重写。

Thus, if you have:

因此,如果你有:

@immplementation Abstract // : NSObject
- (void) doSomethingClassy
{
    [Abstract classyThing];
}

+ (void) classyThing
{
    ... some classy code ...;
}
@end

@interface Concrete : Abstract
@end
@implementation Concrete
+ (void) classyThing
{
    ... some classy code ...;
    [super classThing];
}
@end

Then this won't call Concrete's +classyThing from Abstract's implementation of doSomethingClassy:

那么这就不会从抽象的doSomethingClassy实现中调用Concrete's +classyThing:

[[[Concrete alloc] init] doSomethingClassy];

Whereas if you modify doSomethingClassy to do [[self class] classyThing]; it'll work as expected.

然而,如果你修改doSomethingClassy来做[[self class] classyThing];它会正常工作。

(note that this is a concrete example of Liye Zhang's answer -- feel free to mark his correct as he was first, just not with quite as concrete of an example)

(请注意,这是张丽叶回答的一个具体例子——尽管他的回答是正确的,但他的回答并不像一个例子那么具体)