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

oc-17-description

时间:2016-03-23 06:17:39      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

Book.h

#import <Foundation/Foundation.h>

@interface Book : NSObject
{
    NSString *_bookName; // 书名
    NSString *_author;   // 作者
}
- (void)setBookName:(NSString *)bookName;
- (NSString *)bookName;
- (void)setAuthor:(NSString *)author;
- (NSString *)author;
@end

Book.m

#import "Book.h"

@implementation Book

- (NSString *)description//NSObject的方法,java里面的toString()方法
/**
- (NSString *)description
1.所有的类都有description方法
2.作用:辅助NSLog输出.
<Book: 0x100306750>
类名:对象的地址(%p也可以输出)
*/
{
    NSLog(@"-----");
    return [NSString stringWithFormat:@"%@",self];
}

- (void)setBookName:(NSString *)bookName
{
    _bookName = bookName;
}

- (NSString *)bookName
{
    return _bookName;
}

- (void)setAuthor:(NSString *)author
{
    _author = author;
}

- (NSString *)author
{
    return _author;
}
@end

main.m

#import <Foundation/Foundation.h>
#import "Book.h"
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //str格式化输出
        NSString *str = [NSString stringWithFormat:@"名字是:%@,年龄是:%d,学号是:%d",name,age,no];
        NSLog(@"%@",str);
        //NSLog格式化输出
        NSLog(@"这本书的名字是:%@,作者名字:%@",name,[b author]);
    }
    return 0;
}

 

oc-17-description

标签:

原文地址:http://www.cnblogs.com/yaowen/p/5309356.html

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