类目 延展 单例 协议

//类目
#import
<Foundation/Foundation.h> //声明一个NSString的分栏 @interface NSString (compareValue) - (NSComparisonResult)compareValue:(NSString *)string; @end ---------------------------------- #import "NSString+compareValue.h" @implementation NSString (compareValue) - (NSComparisonResult)compareValue:(NSString *)string{ if (self.integerValue > string.integerValue) { return NSOrderedDescending; } if (self.integerValue < string.integerValue) { return NSOrderedAscending; } return NSOrderedSame; } @end
#import <Foundation/Foundation.h>

@interface Student : NSObject

@property (nonatomic, retain)NSMutableArray *studyInfo;//表示学习科
+ (Student *)bestStudent;

@end

```````````````------------------
/*
 增加类目  已知 类 系统类
 延展  写在m文件私有  可操作类
      写在其他文件  封装
 kvo/kvc
*/
#import "Student.h"
@interface Student ()

@property (nonatomic, assign)NSInteger score;
- (NSInteger)compareScore:(Student *)other;

@end

@implementation Student

+ (Student *)bestStudent;{
    
    static Student *stu = nil;
    
    if (!stu) {
        
        stu = [[Student alloc]init];
        stu.studyInfo = [NSMutableArray array];
    }
    return stu;
}
//标准单例模式会将所有产生新对象导致释放值的方法重写
- (NSInteger)compareScore:(Student *)other{

    return self.score - other.score;
}
@end
------------------------------

#import <Foundation/Foundation.h>

 
  

 

 
  

@interface Person : NSObject

 
  

 

 
  

//应用里面的唯一用户 ,游戏里的主角

 
  

//唯一 、全局不释放、方便获取

 
  

 

 
  

//单例的获取方法

 
  

+ (Person *)heighestPerson;

 
  

 

 
  

@end

 
  

#import "Person.h"

 
  

#import "Student.h"

 
  

 

 
  

@interface Person ()//延展可操作类,提供私有化的方法和属性的申明

 
  

@property (assign, nonatomic)NSInteger age;

 
  

@end

 
  

 

 
  

@implementation Person

 
  

 

 
  

+ (Person *)heighestPerson{

 
  

 

 
  

    //建立静态对象,置为nil

 
  

    static Person *per = nil;

 
  

    

 
  

    if (!per) {//保证使用时初始化保证唯一,保证不释放

 
  

        

 
  

        per = [[Person alloc]init];//保证不释放

 
  

    }

 
  

    return per;

 
  

}

 
  

- (instancetype)init

 
  

{

 
  

    self = [super init];

 
  

    if (self) {

 
  

        

 
  

        NSArray *info = [Student bestStudent].studyInfo;

 
  

        NSLog(@"%@",info);

 
  

    }

 
  

    return self;

 
  

}

 
  

 

 
  

@end

 
//
//将特殊的属性和方法,放到独立的文件中,提高封装行
//提供私有化处理和方便性
#import "Person.h"

@interface Person ()//延展,提供私有化的方法和属性的申明
@property (assign, nonatomic)NSInteger age;
@end
#import <Foundation/Foundation.h>
#import "Working.h"

@interface Developer : NSObject<Working>
//支持协议,必须实现协议所规定的属性和方法
//属性直接声明
@property (nonatomic, assign)NSInteger salary;
//方法不需要声明,只要实现
@end

---------------
#import "Developer.h"

@implementation Developer

- (void)work{

    NSLog(@"work");
}
@end
---------------------
#import <Foundation/Foundation.h>   
//协议

//working协议,规定对象必须有salary(薪资标准)和work的能力
@protocol Working <NSObject>
@property (nonatomic, assign)NSInteger salary;

- (void)work;

@end

 

转载于:https://www.cnblogs.com/myOcNote/p/4531513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值