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; }
#import "Girl.h" @implementation Girl // 2.想要调用自己的对象方法时,通过重新创建对象来调用. + (void)callWithPhone:(Phone *)phone { [phone call]; NSLog(@"女孩要打电话了!"); // 想要一边打电话一边吃东西 Girl *g = [Girl new]; [g eat];//类方法调用对象方法 } - (void)eat { NSLog(@"吃吃吃吃...."); Phone *phone = [Phone new]; [Girl callWithPhone:phone];//对象方法调用类方法 } @end
+ (void)getUp { NSLog(@"起床...."); [self xiShu];//类方法调用类方法 } + (void)xiShu { NSLog(@"刷牙--洗脸"); } @end