码迷,mamicode.com
首页 > 其他好文 > 详细

OC学习笔记-description方法

时间:2015-01-14 00:32:18      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

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

 

OC学习笔记-description方法

标签:

原文地址:http://www.cnblogs.com/loftyspirit/p/4209206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!