标签:
Code:
#import <Foundation/Foundation.h> @interface Person : NSObject @property int age; @property double weight; @end @implementation Person - (NSString*)description { return [NSString stringWithFormat:@"age = %d, weight = %.2f", [self age], [self weight]]; } + (NSString*)description { return @"Person+description"; } @end int main(int argc, char* argv[]) { @autoreleasepool { Person* p = [[Person alloc] init]; [p setAge:20]; [p setWeight:60.00]; // 隐式调用了Person的-(NSString*)description方法 NSLog(@"%@", p); // 隐式调用了Person的+(NSString*)description方法 Class c = [Person class]; NSLog(@"%@", c); } return 0; }
Output:
age = 20, weight = 60.00 Person+description
标签:
原文地址:http://www.cnblogs.com/loftyspirit/p/4209206.html