更新时间:2023-10-21 23:00:22
只需使用[self class]
!当您在Objective-C中调用类方法时,self
将指示正在调用哪个类.例如:
Just use [self class]
! When you call a class method in Objective-C, self
will indicate which class is calling. For example:
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface A: NSObject
+ (void)foo;
@end
@implementation A
+ (void)foo {
printf("%s called!", [[[self class] description] UTF8String]);
}
@end
@interface B: A @end
@implementation B @end
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[A foo];
[B foo];
[pool release];
return 0;
}
应打印
A called!
B called!