Object-c初步学习 四

1.category的用法 不修改对象源文件的方式下,给类添加方法
为Student类添加方法

//
//  NSObject+StudyCategory.h
//

#import <Foundation/Foundation.h>
#import "Student.h"

#pragma mark 类名后面的(test)代表Category,可以在不修改原来的类文件的情况下,添加新的方法

//pragma mark - 不可以添加新的成员变量

@interface Student (StudyCategory)

- (void)test:(int)a;

@end

//
//  NSObject+StudyCategory.m
//

#import "Student+StudyCategory.h"
#import "Student.h"

@implementation Student (StudyCategory)

- (void)test:(int)a {
    NSLog(@"Sutdent Category test add %i",a);
}

@end

这样就可以为类Student添加test:方法

外部可直接使用

 Student *stu = [[[Student alloc] init] autorelease];
[stu test:1];

2.protocol的用法 这个是代理,类似java中的interface接口

//
//  StudyProtocol.h
//

#import <Foundation/Foundation.h>
@class Button;

#pragma mark <>代表protocol
@protocol StudyProtocol <NSObject>

- (void)onClick:(Button *)btn;

@end

//
//  Button.h
//

#import <Foundation/Foundation.h>

@protocol StudyProtocol;

@interface Button : NSObject

@property (nonatomic,retain) id<StudyProtocol> delegate;

- (void)testClick;

@end

//
//  Button.m
//

#import <Foundation/Foundation.h>
#import "Button.h"
#import "StudyProtocol.h"

@implementation Button

- (void)testClick{
    //判断有没有当前方法
    if([_delegate respondsToSelector:@selector(onClick:)]){
        [_delegate onClick:self];
    }else{
        [_delegate onClick];
    }
}

- (void)dealloc{
    [_delegate release];
    
    [super dealloc];
}

@end
 

//
//  NSObject+ButtonListener.h
//

#import <Foundation/Foundation.h>

#import "StudyProtocol.h"

NS_ASSUME_NONNULL_BEGIN


@interface ButtonListener : NSObject <StudyProtocol>


@end

NS_ASSUME_NONNULL_END
 

//
//  ButtonListener.m
//

#import <Foundation/Foundation.h>

#import "ButtonListener.h"

@class Button;

@implementation ButtonListener

- (void)onClick:(Button *)btn{
    NSLog(@"button is click %@",btn);
}

@end
 

使用方法类似java的接口回调机制

     //protocol的用法
        Button *btn = [[Button alloc] init];
        
        ButtonListener *listener = [[ButtonListener alloc]init];
        //设置监听器
        [btn setDelegate: listener];
        
        //模拟点击
        [btn testClick];

相关推荐

  1. Object-c初步学习

    2024-01-09 18:38:01       33 阅读
  2. Object-c初步学习

    2024-01-09 18:38:01       36 阅读
  3. Effective Objective-C 学习

    2024-01-09 18:38:01       29 阅读
  4. Objective-C学习计划

    2024-01-09 18:38:01       9 阅读
  5. C++初学教程

    2024-01-09 18:38:01       38 阅读
  6. Objective-C 学习笔记 | 范畴

    2024-01-09 18:38:01       7 阅读
  7. <span style='color:red;'>c</span>++<span style='color:red;'>初步</span>

    c++初步

    2024-01-09 18:38:01      16 阅读
  8. Effective Objective-C学习第一周

    2024-01-09 18:38:01       29 阅读
  9. Effective Objective-C 学习第二周

    2024-01-09 18:38:01       26 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-09 18:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-09 18:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-09 18:38:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-09 18:38:01       20 阅读

热门阅读

  1. 【低功耗】芯片低功耗-硬件

    2024-01-09 18:38:01       39 阅读
  2. 彻底卸载Microsoft Edge:一步步指南

    2024-01-09 18:38:01       42 阅读
  3. 骑砍战团MOD开发(34)-光照系统

    2024-01-09 18:38:01       45 阅读
  4. ctemplate的使用

    2024-01-09 18:38:01       34 阅读
  5. docker离线安装教程

    2024-01-09 18:38:01       41 阅读
  6. Linux系统中MYSQL重置密码(针对root忘记密码)

    2024-01-09 18:38:01       40 阅读
  7. MySQL 8.0中新增的功能(六)

    2024-01-09 18:38:01       30 阅读