首页 > 其他 > 详细

oc-17-description

时间:2016-03-23 06:17:09      阅读:137      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!