[关闭]
@fiy-fish 2015-07-14T19:49:02.000000Z 字数 1992 阅读 1194

day02-04-扑克牌

Objective-c


  1. // main.m
  2. // day02-04-扑克牌
  3. //
  4. // Created by Aaron on 15/7/2.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. //左手和右手的扑克牌进行交换
  8. #import <Foundation/Foundation.h>
  9. #import "Pocker.h"
  10. #import "Person.h"
  11. int main(int argc, const char * argv[]) {
  12. @autoreleasepool {
  13. Pocker *p1 = [[Pocker alloc] initWithName:@"♥10"];
  14. //%@ 输出字符串对象
  15. //NSLog(@"---%@",[p1 name]);
  16. Person *person = [[Person alloc] init];
  17. [person leftHandCatchPocker:p1];
  18. //[person showPocker];
  19. Pocker *p2 = [[Pocker alloc] initWithName:@"😎"];
  20. [person rightHandCatchPocker:p2];
  21. [person showPocker];
  22. [person changePocker];
  23. [person showPocker];
  24. }
  25. return 0;
  26. }

  1. #import <Foundation/Foundation.h>
  2. #import "Hand.h"
  3. @interface Person : NSObject
  4. {
  5. Hand *_leftHand;
  6. Hand *_rightHand;
  7. }
  8. -(void)leftHandCatchPocker:(Pocker *)p;
  9. -(void)rightHandCatchPocker:(Pocker *)p;
  10. -(void)changePocker;
  11. -(void)showPocker;
  12. @end

  1. #import "Person.h"
  2. @implementation Person
  3. -(instancetype)init
  4. {
  5. //可以不初始化么,这里初始化成了什么值
  6. if(self = [super init])
  7. {
  8. _leftHand = [[Hand alloc] init];
  9. _rightHand = [[Hand alloc] init];
  10. }
  11. return self;
  12. }
  13. -(void)leftHandCatchPocker:(Pocker *)p
  14. {
  15. //可以调用其他类的对象方法?
  16. [_leftHand setMyPocker:p];//变量可以调用方法?
  17. }
  18. -(void)rightHandCatchPocker:(Pocker *)p
  19. {
  20. [_rightHand setMyPocker:p];
  21. }
  22. -(void)changePocker
  23. {
  24. Pocker *temp = [_leftHand myPocker];
  25. [_leftHand setMyPocker:[_rightHand myPocker]];
  26. [_rightHand setMyPocker:temp];
  27. }
  28. -(void)showPocker
  29. {
  30. NSLog(@"左手上的牌是:%@",
  31. [[_leftHand myPocker] name]);
  32. NSLog(@"右手上的牌是:%@",
  33. [[_rightHand myPocker] name]);
  34. }
  35. @end

  1. #import <Foundation/Foundation.h>
  2. //记得包含“Pocker.h”头文件
  3. #import "Pocker.h"
  4. @interface Hand : NSObject
  5. {
  6. Pocker *_myPocker;// 带*号
  7. }
  8. -(void)setMyPocker:(Pocker *)p;
  9. -(Pocker *)myPocker;
  10. @end

  1. #import "Hand.h"
  2. @implementation Hand
  3. -(void)setMyPocker:(Pocker *)p
  4. {
  5. //为什么要判断
  6. if(_myPocker != p)
  7. {
  8. _myPocker = p;
  9. }
  10. }
  11. -(Pocker *)myPocker
  12. {
  13. return _myPocker;
  14. }
  15. @end

  1. #import <Foundation/Foundation.h>
  2. @interface Pocker : NSObject
  3. {
  4. NSString *_name;//申明字符串一定要带*号?
  5. }
  6. -(instancetype)initWithName:(NSString *)name;
  7. -(NSString *)name;
  8. @end

  1. #import "Pocker.h"
  2. @implementation Pocker
  3. -(instancetype)initWithName:(NSString *)name
  4. {
  5. //如果不初始化是什么后果
  6. if(self = [super init])
  7. {
  8. _name = name;
  9. }
  10. return self;
  11. }
  12. -(NSString *)name
  13. {
  14. return _name;
  15. }
  16. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注