这个题目已经通过多开发完成了对象方法和类方法!!! 怎么实现对象方法调用类方法 和 类方法调用对象方法.
/*
题目: 学生用电脑玩游戏:
一个学生类Person; 一个电脑类Computer;
你能想出多少种实现思路?
*/
#import <Foundation/Foundation.h> #import "StudentPlayer.h" #import "Computer.h"
int main(int argc, const char * argv[]) { @autoreleasepool {
NSLog(@"学生 %@ 正在玩电脑游戏 %@",[StudentPlayer Student:@"孙胜"],[Computer gameName:@"LOL"]); } return 0; } ----------------------------------------------------------------------------
#import <Foundation/Foundation.h>
@interface Computer : NSObject //{ // NSString *_game; // //}
+(NSString *)gameName:(NSString *)gameName; //+(NSString *)gameName;
@end
----------------------------------------------------
#import "Computer.h"
//类方法 @implementation Computer
+(NSString *)gameName:(NSString *)gameName { return gameName; } @end
-------------------------------------------------------
#import <Foundation/Foundation.h> #import "Computer.h"
//类方法 @interface StudentPlayer : NSObject +(NSString *)Student:(NSString *)stuName; @end -----------------------------------------------------------
#import "StudentPlayer.h"
//类方法 @implementation StudentPlayer +(NSString *)Student:(NSString *)stuName { return stuName; } @end
|