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; }
时间: 2024-10-10 21:12:33