[关闭]
@fiy-fish 2015-07-18T19:44:15.000000Z 字数 1380 阅读 1307

day06-05-多态-按钮练习

Objective-c


  1. #import <Foundation/Foundation.h>
  2. #import "MyButton.h"
  3. #import "MyButton_a.h"
  4. #import "MyButton_b.h"
  5. #import "MyButton_AA.h"
  6. #import "MyButton_BB.h"
  7. //创建四个按钮分别是 a A b B
  8. //输入a A b B 其中一种, 让对应的按钮输出 轻拳 重拳 轻腿 重腿
  9. int main(int argc, const char * argv[]) {
  10. @autoreleasepool {
  11. //MyButton *btn = [[MyButton alloc] init];
  12. MyButton_a *a = [[MyButton_a alloc] init];
  13. MyButton_AA *A = [[MyButton_AA alloc] init];
  14. MyButton_b *b = [[MyButton_b alloc] init];
  15. MyButton_BB *B = [[MyButton_BB alloc] init];
  16. NSDictionary *dic = @{@"a":a,@"A":A,@"b":b,@"B":B};
  17. while(1)
  18. {
  19. char s[10] = {};
  20. scanf("%s",s);
  21. MyButton *btn = dic[[NSString stringWithUTF8String:s]];
  22. [btn btnClick];
  23. }
  24. }
  25. return 0;
  26. }

  1. #import "MyButton.h"
  2. @interface MyButton_AA : MyButton
  3. @end

  1. #import "MyButton_AA.h"
  2. @implementation MyButton_AA
  3. -(void)btnClick
  4. {
  5. NSLog(@"重拳");
  6. }
  7. @end

  1. #import "MyButton.h"
  2. @interface MyButton_b : MyButton
  3. @end

  1. #import "MyButton_b.h"
  2. @implementation MyButton_b
  3. -(void)btnClick
  4. {
  5. NSLog(@"轻腿");
  6. }
  7. @end

  1. #import "MyButton.h"
  2. @interface MyButton_BB : MyButton
  3. @end

  1. #import "MyButton_BB.h"
  2. @implementation MyButton_BB
  3. -(void)btnClick
  4. {
  5. NSLog(@"重腿");
  6. }
  7. @end

  1. #import "MyButton.h"
  2. @interface MyButton_a : MyButton
  3. @end

  1. #import "MyButton_a.h"
  2. @implementation MyButton_a
  3. -(void)btnClick
  4. {
  5. NSLog(@"轻拳");
  6. }
  7. @end

  1. #import <Foundation/Foundation.h>
  2. @interface MyButton : NSObject
  3. {
  4. NSString *_name;
  5. }
  6. -(void)btnClick;
  7. @end

  1. #import "MyButton.h"
  2. @implementation MyButton
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. _name = @"a";
  8. }
  9. return self;
  10. }
  11. -(void)btnClick
  12. {
  13. NSLog(@"被点击");
  14. }
  15. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注