iOS前期OC训练OC_07类的扩展

本文介绍Objective-C中的类扩展和协议使用方法。包括如何通过类扩展为现有类添加新功能,以及如何定义和实现协议以促进类间的交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  main.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "NSString+StringMethod.h"

#import "Student.h"

#import "Boy.h"

#import "Girl.h"

#import "Man.h"

#import "Mother.h"

#import "Nurse.h"

int main(int argc, const char * argv[]) {

//    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"2", @"3", @"4", @"5", @"6", nil];

//    //

//    [dic setValue:nil forKey:@"2"];

//    NSLog(@"%@", dic);

//    

    /// 类目

//    // 系统已经写好的类目:按照功能对系统的类方法进行区分

//    // 类目从@interface开始,后面是当前类的名字,类名后是分类的功能,@end结束

//    // 我们创建的类目,一般是为了把一些系统的类进行功能扩充

//    

//    NSString *str = @"我是火星人";

//    [str sayHi];

//

//    /// 判断两个时间是否是一个月

//    NSString *str = @"2015-07-12 19:00:00";

//    NSString *str2 = @"2015-07-12 20:00:00";

//    if ([str2 isEqualToDate:str]) {

//        NSLog(@"月份相同");

//    } else {

//        NSLog(@"月份不同");

//    }

//    NSLog([str appendTwoDate:str2]);

//

//    NSDate *date = [str stringToDate];

//    NSLog(@"%@", date);

//    

//    NSDate *date1 = [NSString stringToDate:str];

//    NSLog(@"%@", date1);

    

//    Student *stu = [[Student alloc] init];

//    stu.stuSex = @"";

//    NSLog(@"%@", stu.stuSex);


//    // 创建两个小人

//    Boy *Adam = [[Boy alloc] init];

//    Girl *Eva = [[Girl alloc] init];

//    

//    // 5.设置代理人

//    Eva.delegate = Adam;

//    // 通过调用结婚的方法,来执行内部的协议方法

//    [Eva getMarry];

//    // 5. 设置代理人

//    Man *wangliqian = [[Man alloc] init];

//    Eva.delegate = wangliqian;

//    [Eva getMarry];

    

    Mother *Smith = [[Mother alloc] init];

    Nurse *Junbaobao = [[Nurse alloc] init];

    Smith.delegate = Junbaobao;

    [Smith hire];

   

    return 0;

    

}



//

//  NSString+StringMethod.h

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface NSString (StringMethod)


- (void)sayHi;


// 输入两个日期,判断两个日期是否在同一个月

- (BOOL)isEqualToDate:(NSString *)otherDate;


// 拼接两个日期

- (NSString *)appendTwoDate:(NSString *)otherDate;

// 给定两个时间,判断时间是否在这两个时间区间内

- (BOOL)isInTimeZone:(NSString *)beginTime

                 end:(NSString*)endTime;


// 传过去一个字符串,返回一个时间的对象NSDate类型

- (NSDate *)stringToDate;

// 类方法

+ (NSDate *)stringToDate:(NSString *)dateStr;

@end


//

//  NSString+StringMethod.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import "NSString+StringMethod.h"


@implementation NSString (StringMethod)


- (void)sayHi

{

    NSLog(@"你好愚蠢的地球人");

}


- (BOOL)isEqualToDate:(NSString *)otherDate

{

    // 这个方法需要两个参数,第一个参数就是方法的调用者,谁调用了这个方法,那么在这个方法的内部,self就是使用方法的那个对象

//        NSLog(@"%@", self);

    // 进行截取

    NSString *month1 = [self substringWithRange:NSMakeRange(5, 2)];

    NSString *month2 = [otherDate substringWithRange:NSMakeRange(5, 2)];

    return [month1 isEqualToString:month2];


//    [self sayHi];

    

//    return YES;

    

}


- (NSString *)appendTwoDate:(NSString *)otherDate

{

    // 先截取

    NSString *newDate1 = [self substringWithRange:NSMakeRange(0, self.length - 2)];

    NSString *newDate2 = [otherDate substringWithRange:NSMakeRange(otherDate.length - 8, 5)];

    NSString *newMonth = [NSString stringWithFormat:@"%@-%@", newDate1, newDate2];

    return newMonth;

}


// 这种方法称为私有方法,不让类外部去使用,只能类内部去使用,想让类外面用的方法一概在.h声明.如果想在主函数使用只要在.h文件中声明即可

- (void) junbaobaotaizangle

{

    NSLog(@"我是喵星人");

}


- (BOOL)isInTimeZone:(NSString *)beginTime

                 end:(NSString*)endTime

{

    return YES;

}



- (NSDate *)stringToDate

{

    // 设置格式

    NSDateFormatter *formater = [[NSDateFormatter alloc] init];

    [formater setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    return [formater dateFromString:self];

}


+ (NSDate *)stringToDate:(NSString *)dateStr

{

    NSDateFormatter *formater = [[NSDateFormatter alloc] init];

    [formater setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    return [formater dateFromString:dateStr];

}


@end


//

//  Student.h

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Student : NSObject


@property(nonatomic, copy)NSString *stuSex;


@end


//

//  Student.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import "Student.h"


// 延展

// 延展以@interface开头,然后写当前延展的类名,类名后加一个(),@end结束

// 一般延展会写在自己写的.m文件中,把一些不想让另外部分调用的属性放在延展里,这样这条属性只能在类的内部使用,外部使用不了,尽最大的可能保护当前类的安全

// 类目一般都是给看不见.m的系统文件进行扩展,延展一般是针对自己的类进行操作

@interface Student()


@property(nonatomic, copy)NSString *name;


@end

@implementation Student


- (void)create

{

    _name = @"你好";

    NSLog(@"%@", _name);

}


@end


//

//  Boy.h

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Girl.h"


// 4.引完头文件之后,boy需要签到协议

@interface Boy : NSObject<Marry>



@end


//

//  Boy.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import "Boy.h"


@implementation Boy


// 6.实现协议的方法makeMoney

- (void)makeMoney

{

    NSLog(@"买买买");

}

@end


//

//  Girl.h

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>


// 1.声明一份协议

// @protocol 协议的关键词

@protocol Marry <NSObject>

// 协议的内容

// 协议方法的声明

// @required 是必须实现的方法,默认是必须实现的方法

// @optional 是可选择执行的方法

@required

- (void)makeMoney;


@optional

- (void)cook;


@end


@interface Girl : NSObject

// 2.设置代理人的属性

@property(nonatomic, assign)id<Marry>delegate;

// 两个人结婚之后,协议才正式的生效

- (void)getMarry;


@end


 //

//  Girl.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import "Girl.h"


@implementation Girl


- (void)getMarry

{

    // 3.让代理人去执行协议里的方法

    [self.delegate makeMoney];

}

@end


//

//  Man.h

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Girl.h"

// 4.签订协议


@interface Man : NSObject<Marry>


@end


//

//  Man.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import "Man.h"


@implementation Man

- (void)makeMoney

{

    NSLog(@"随便花");

}

@end


//

//  Mother.h

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>


// 1.声明一份协议

// @protocol 协议的关键词

@protocol MotherDelegate <NSObject>


@required 

- (void)childcare;

- (void)cook;

@end


@interface Mother : NSObject

// 2.设置代理人的属性

@property(nonatomic, assign)id<MotherDelegate>delegate;

- (void)hire;

@end


//

//  Mother.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import "Mother.h"


@implementation Mother


- (void)hire

{

    [self.delegate childcare];

    [self.delegate cook];

}

@end


//

//  Nurse.h

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h> // <>系统的

#import "Mother.h"                // ""自己定义的

@interface Nurse : NSObject<MotherDelegate>


@end


//

//  Nurse.m

//  OC07_类的扩展

//

//  Created by dllo on 15/7/24.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import "Nurse.h"


@implementation Nurse

- (void)childcare

{

    NSLog(@"照顾孩子");

}


- (void)cook

{

    NSLog(@"做饭");

}

@end






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值